DBContext查询优化Entity Framework

huangapple go评论75阅读模式
英文:

DBContext Query Optimization Entity Framework

问题

以下是翻译好的部分:

只是一个新手,我们正在启动一个新项目,
现在我的问题是,我们正在使用实体框架 dbcontext。
有没有方法或最佳实践来最小化选择语句,
例如 context.tbEmployee.where(p=>p.name=='test').tolist()
问题在于,它选择了所有列,即使我不需要其他列,我知道你会告诉我指定你需要的列,
但我的问题是,并不是每次我们都对代码非常复杂
只是想要一个关于该怎么做的建议或最佳实践。
我应该使用一个模型,然后映射到实体表
你有什么建议?

只是一个建议或最佳实践或如何最小化完整选择语句的模式。

英文:

Just a newbie here, We are starting a new Project,
Now my question, We are using entity framework dbcontext.
Is there way or best practice to minimize the select statement ,
for e.g context.tbEmployee.where(p=>p.name=='test').tolist()
the issue here is that, It picks up all the column even i don't
need the other columns, I know you would tell that specify the column that you need
but my issue is that not every time we people are very intricate with the codes
Just want to have a suggestion or best practice on what to do.
Should i use a Model then map to the Entity tables
what do you suggest?

Just a suggestion or best practice or what pattern to use on how minimize the full select statement

答案1

得分: 2

如Matthew已经建议的,您可以使用Select()函数来减少查询的列。

最简单的方法是使用匿名类型。例如,如果您只想要员工的Id和Name,您可以编写以下代码:

var persons = context.tbEmployee.Where(p=> p.name == "test")
                                .Select(p => new {Id = p.Id, Name = p.Name})
                                .ToList()

然后您可以通过 persons.First().Name 访问Name。

您甚至可以进一步,为这个“记录”声明一个类型

public class NamedId
{
    public int Id {get;set;}
    public string Name {get;set;}
}

然后返回该类型的列表

var persons = context.tbEmployee.Where(p=> p.name == "test")
                                .Select(p=> new NamedId {Id = p.Id, Name = p.Name})
                                .ToList()

如果您的数据库对这些列有索引,这样做可能会带来巨大的性能优势。

英文:

As Matthew already suggested, you can use the Select() function to reduce the queried columns.

The easiest way is to get a anonymous type. For example if you just want the Id and the Name of your Employee, you could write the following:

var persons = context.tbEmployee.Where( p=> p.name == "test")
                                .Select(p => new {Id = p.Id, Name = p.Name})
                                .ToList()

Then you can access the Name with persons.First().Name

You could go event further and declare a Type for that "record"

public class NamedId
{
    public int Id {get;set;}
    public string Name {get;set;}
}

And then return a list of that type

var persons = context.tbEmployee.Where(p=> p.name == "test")
                                .Select(p=> new NamedId {Id = p.Id, Name = p.Name})
                                .ToList()

This can have huge performance benefits, if youre database has an index for these columns.

答案2

得分: 0

使用".Select( s => new { s.X, s.Y, ... } )"方法来选择所需的列。

您可以指定一个类型来在当前方法之外使用结果: ".Select( s => new YourType(s.X, s.Y, ...) ).ToList()"。它将生成List

英文:

Use ".Select( s => new { s.X, s.Y, ... } )" method to select required columns.

You can specify a type to use results out of the current method: ".Select( s => new YourType(s.X, s.Y, ...) ).ToList()". It will produce List<YourType>.

huangapple
  • 本文由 发表于 2023年6月15日 20:07:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定