简体   繁体   English

选择新的 Linq where 子句

[英]Linq where clause in select new

I have 2 tables let's call them t1 and t2.我有 2 张表,我们称它们为 t1 和 t2。 t1 has an ID, a string and a boolean. t1 有一个 ID、一个字符串和一个布尔值。 t2 has an ID, a date and a t1.ID. t2 有一个 ID、一个日期和一个 t1.ID。 I want to put them in a List of my class.我想把它们放在我的班级列表中。

public class MyClass {
    public string Name {get; set;}
    public DateTime Date1 {get; set;}
    public Datetime Date2 {get; set;}
}

(it has 15 dates, to keep it smaller I just put two in there) (它有 15 个日期,为了让它更小,我只在里面放了两个)

I was able to find a solution :我找到了解决方案:

List<MyClass> list = (from t2 in con.t2
                      join t1 in con.t1 on t2.T1ID equals t1.ID
                      select t2).GroupBy(d => d.T1ID).Select(x => new MyClass()
{
    Name = x.FirstOrDefault().t1.Name,
    Date1 = x.where(d => d.ID == 1).SingleOrDefault().Date,
    Date2 = x.where(d => d.ID == 2).SingleOrDefault().Date
}).ToList();

It is working as expected, but the problem is, I'm getting this warning from visual studio:它按预期工作,但问题是,我从 Visual Studio 收到此警告:

The LINQ expression 'GroupBy([t2].T1ID)' could not be translated and will be evaluated locally. LINQ 表达式“GroupBy([t2].T1ID)”无法翻译,将在本地进行评估。

It appears several times.它出现了好几次。 This message comes for SingleOrDefault too.此消息也适用于 SingleOrDefault。 I tested a little bit around and it just comes if I put the where in the select new.我测试了一点,如果我把 where 放在 select new 中,它就会出现。

So how do I change my expression to avoid that?那么我如何改变我的表达来避免这种情况呢? Or is my expression crap and I could've done it much better?还是我的表情很垃圾,我本来可以做得更好? (I can't change the table structure) (我不能改变表结构)

I believe this is still a limitation of EF.我相信这仍然是EF的一个限制。 Just pay attention to the memory used by your queries because the warning is alerting you to do so.只需注意查询使用的内存,因为警告会提醒您这样做。

Regards.问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM