Linq works in one statement, but not with a choice in a property

I have an object Dcreated from automatic table matching D. I added the following property to it in a partial class.

public Address PhysicalAddress
{
   get { return this.Addresses.FirstOrDefault(a => a.AddrType == "PHY"); }
}

This works great on it.

I would like to write the following linq query on it:

var result = from d in _db.D
    where d.PhysicalAddress.State == addr.State
    select d;

What does not work. It throws NotSupportedExceptionwhen I try to iterate over result.

However, the following works:

var result = from d in _db.D
    where d.Addresses.Single(dAddr => dAddr.AddrType == "PHY").State == addr.State
    select d;

Why does Linq to Sql work this way? Is there a way to rewrite my property so that it works?

+3
source share
2 answers

LinQ SQL, db. PhysicalAddress , , , , Linq can not DB . linq to sql .

, , EntityRef.

+3

, where, SQL, .

, ! , , .ToList()! , desasterous .

var result = _db.D.ToList().Where(d => d.PhysicalAddress.State == addr.State); 

var result = from d in _db.D.ToList()
    where d.PhysicalAddress.State == addr.State
    select d;
+2

Source: https://habr.com/ru/post/1734974/


All Articles