Using Entity Framework Version=6.0.0.0 to get to get common id and orderid as shown below.
var dt1 = from p in dt.AsEnumerable()
select new
{
Id = p.Field<int>("Id"),
OrderId = p.Field<int>("OrderId")
};
var dt2 = (from order in db.Orders
select new
{
order.Id,
order.OrderId
}).ToList();
var intersect = dt1.Intersect(dt2);
Based on the list of values in intersect. I need to select all the values from Orders Table.
Trying to used code getting error "unable to create a constant value of type anonymous type only primitive types"
var result= (from a in sync.Orders
where intersect.Any(b => a.Id == b.Id && a.OrderId == b.OrderId)
select a).ToList();
Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context. Unable to create a constant value of type 'DataAccess.Posts'.
Only primitive types or enumeration types are supported in this context. Unable to create a constant value of type 'DataAccess.Posts'. Only primitive types or enumeration types are supported in this context.
You can't have a strong type that has a property of an anonymous type. Change your owners query to return a collection of Owner objects or create a separate strong type that contains just the properties you want. I'm getting the exception "Unable to create a constant value of type 'Anonymous type'.
Unable to create a constant value of type API.Models.PersonProtocol. Only primitive types or enumeration types are supported in this context ppCombined below is an IEnumerable object of PersonProtocolType, which is constructed by concat of 2 PersonProtocol lists.
You are trying to "join" an EF query with an in-memory data set, which does not work because there's not a way to embed the list and the lookup in SQL. One option is to pull the entire table into memory with AsEnumerable
:
var result= (from a in sync.Orders.AsEnumberable
where intersect.Any(b => a.Id == b.Id && a.OrderId == b.OrderId)
select a).ToList();
Another option is to concatenate the Id
and OrderId
into one value and use Contains
since that can be translated to an IN
clause in SQL:
var lookup = intersect.Select(i => i.Id + "-" + i.OrderId).ToList();
var result= (from a in sync.Orders
where lookup.Contains(a.Id + "-" + a.OrderId)
select a).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With