Tyimi
Tyimi

Reputation: 31

Linq where clause in select new

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)

Upvotes: 2

Views: 464

Answers (1)

Leandro Andrioli
Leandro Andrioli

Reputation: 31

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.

Upvotes: 3

Related Questions