Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to create a constant value of type anonymous type only primitive types

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();
like image 763
Nirav Parmar Avatar asked Jun 25 '15 15:06

Nirav Parmar


People also ask

Is it possible to create a constant value of an anonymous type?

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'.

Is it possible to create a constant value of type'DataAccess'?

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.

How to create a strong type with an anonymous type?

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'.

Is it possible to create a constant value of type personprotocoltype?

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.


1 Answers

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();
like image 74
D Stanley Avatar answered Oct 24 '22 17:10

D Stanley