c# – 使用LINQ插入选择 – 在查询中绕过实体构造

c# – 使用LINQ插入选择 – 在查询中绕过实体构造,第1张

概述我已经阅读了几个问题,因为错误“不允许在查询中显式构造实体类型”,以及解决它的各种方法. > Explicit construction of entity type ‘###’ in query is not allowed. > Explicit construction of entity type [MyClass] in query is not allowed > Explicit c 我已经阅读了几个问题,因为错误“不允许在查询中显式构造实体类型”,以及解决它的各种方法.

> Explicit construction of entity type ‘###’ in query is not allowed.
> Explicit construction of entity type [MyClass] in query is not allowed
> Explicit construction of entity type ” in query is not allowed

我在我的代码中使用DBML自动生成的liNQ to sql类,因此能够适当地选择和插入数据会很棒.这是另一篇文章中提出的一种方法;在下面的示例中,e_activeSession是DataContext中表的自动生成表示:

var statistics =    from record in startTimes    group record by record.startTime into g    select new e_activeSession            {                workerID = wcopy,startTime = g.Key.GetValueOrDefault(),totalTasks = g.Count(),totalTime = g.Max(o => o.record.timeInSession).GetValueOrDefault(),minDWell = g.Min(o => o.record.DWellTime).GetValueOrDefault(),maxDWell = g.Max(o => o.record.DWellTime).GetValueOrDefault(),avgDWell = g.Average(o => o.record.DWellTime).GetValueOrDefault(),stdevDWell = g.Select(o => Convert.Todouble(o.record.DWellTime)).StdDev(),total80 = g.Sum(o => Convert.ToInt16(o.record.correct80) + Convert.ToInt16(o.record.wrong80)),correct80 = g.Sum(o => Convert.ToInt16(o.record.correct80)),percent80 = Convert.Todouble(g.Sum(o => Convert.ToInt16(o.record.correct80))) /                            g.Sum(o => Convert.ToInt16(o.record.correct80) + Convert.ToInt16(o.record.wrong80))            };

抛出错误,所以我尝试了以下内容:

var groups =    from record in startTimes    group record by record.startTime    into g    select g;var statistics = groups.ToList().Select(    g => new e_activeSession             {                 workerID = wcopy,percent80 = Convert.Todouble(g.Sum(o => Convert.ToInt16(o.record.correct80))) /                             g.Sum(o => Convert.ToInt16(o.record.correct80) + Convert.ToInt16(o.record.wrong80))             });

但是,ToList似乎效率极低,只是让我的代码在那里停留了很长时间.有一个更好的方法吗?

解决方法 AsEnumerable()在处理linq-to-objects方面会做与ToList()相同的事情,但不会浪费时间和内存来存储所有这些内容.相反,当您遍历它时,它将一次创建一个对象.

作为一项规则,您应该使用AsEnumerable()将 *** 作从另一个源移动到内存而不是ToList(),除非您真的想要一个列表(例如,如果您将多次访问相同的数据,那么该列表将作为一个缓存).

所以到目前为止我们有:

var statistics = (  from record in startTimes  group record by record.startTime  into g  select g;  ).AsEnumerable().Select(    g => new e_activeSession    {      workerID = wcopy,/* ... */     });

但是有一个更大的问题.你也要小心小组.当与聚合方法一起完成时,它通常是可以的,但是否则它最终会变成许多数据库调用(一个用于获取键的不同值,然后每个值一个).

考虑到上述情况(我的意思是不提每一栏).不使用AsEnumerable()(或ToList()或你有什么),因为wcopy可能完全在查询之外(我无法看到它的定义),第一个生成的sql将是(如果允许的话),就像是:

select startTime,count(ID),max(timeInSession),/* ... */from tasksgroup by startTime

哪个应该由数据库非常有效地处理(如果不是,请检查索引并在生成的查询上运行数据库引擎优化顾问).

虽然在内存中进行分组,但它可能首先执行:

select distinct startTime from tasks

然后

select timeInSession,/* ... */from taskswhere startTime = @p0

对于找到的每个不同的startTime,将其作为@ p0传递.无论代码的其余部分有多高效,这都会很快变得灾难性.

我们有两个选择.哪个是最好的因情况而异,所以我会给两个,尽管第二个是最有效的.

有时我们最好的方法是加载所有相关的行并在内存中进行分组:

var statistics =  from record in startTimes.AsEnumerable()  group record by record.startTime  into g  select new e_activeSession  {    workerID = wcopy,/* ... */  };

我们可以通过仅选择我们关心的列来使其更有效率(如果以上使用表格中的每一列,则无关紧要)

var statistics =  from record in (    from dbRec in startTimes    select new {dbRec.startTime,dbRec.timeInSession,/*...*/}).AsEnumerable()    group record by record.startTime    into g    select new e_activeSession    {      workerID = wcopy,/* ... */    };

我认为这不是最好的情况.我会在我要枚举组的情况下使用它,然后枚举每个组.在您对每个组执行聚合并且不通过它们进行枚举的情况下,最好将该聚合保留在数据库中.数据库擅长于它们,它将大大减少通过线路发送的数据总量.在这种情况下,我能想到的最好的方法是强制除了镜像它的实体类型之外的新对象,但是它不被识别为实体.你可以为此创建一个类型(如果你在这上面做了几个变种就很有用),否则只需使用匿名类型:

var statistics = (  from record in startTimes  group record by record.startTime  into g  select new{    startTime = g.Key.GetValueOrDefault(),/* ... */  }).AsEnumerable().Select(    d => new e_activeSession    {      workerID = wcopy,startTime = d.startTime,totalTasks = d.totalTasks,/* ... */    });

明显的缺点是纯粹的冗长.但是,它会在db中保持最佳 *** 作,同时仍然不会像ToList()那样浪费时间和内存,不会反复击中db,并将e_activeSession创建拖出linq2sql并进入linq2objects,所以它应该被允许.

(顺便提一下,.NET中的约定是类和成员名称以大写字母开头.没有技术原因,但这样做意味着你将匹配更多人的代码,包括你使用的BCL和其他库的代码).

编辑:第二次偶然;我刚看到你的另一个问题.请注意,在某种程度上,AsEnumerable()在这里是一个变体,正是导致该问题的原因. Grok,你会对不同的linq查询提供者之间的界限进行很多讨论.

总结

以上是内存溢出为你收集整理的c# – 使用LINQ插入/选择 – 在查询中绕过实体构造全部内容,希望文章能够帮你解决c# – 使用LINQ插入/选择 – 在查询中绕过实体构造所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1234838.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存