Linq where clause in select new

c# entity-framework-core linq

Question

I have 2 tables let's call them t1 and t2. t1 has an ID, a string and a boolean. t2 has an ID, a date and a 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)

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:

The LINQ expression 'GroupBy([t2].T1ID)' could not be translated and will be evaluated locally.

It appears several times. This message comes for SingleOrDefault too. I tested a little bit around and it just comes if I put the where in the 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)

1
2
2/27/2019 2:29:26 PM

Popular Answer

I believe this is still a limitation of EF. Just pay attention to the memory used by your queries because the warning is alerting you to do so.

Regards.

2
2/27/2019 3:20:46 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow