How to Create a Constant Value - Only Primitive Types

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

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();

LINQ Query error: Unable to create a constant value of type. Only primitive types or enumeration types are supported in this context

endedContracts is an in memory list and cannot be used directly in this query. Instead, get the value you need outside of the query, for example:

//Get the code here
var brokerCode = endedContracts.First().Broker.Code;

var crystallisedCommissions = _context.Contracts
.Where(x => x.Statement.Sent)
.Where(x => x.Statement.Broker == brokerCode) //Use the code here
.Where(x => !Period.IsPeriodBeforeReferencePeriod(x.Statement.Period, CUT_OFF_PERIOD))
.Where(x => endedContracts.Any(y => y.Contract.Identifier == x.Identifier
&& y.Contract.StartDate == x.ContractStartDate
&& y.Contract.EndDate == x.ContractEndDate)).ToList();

Unable to create a constant value of type Only primitive types or enumeration types are supported in this context

This cannot work because ppCombined is a collection of objects in memory and you cannot join a set of data in the database with another set of data that is in memory. You can try instead to extract the filtered items personProtocol of the ppCombined collection in memory after you have retrieved the other properties from the database:

var persons = db.Favorites
.Where(f => f.userId == userId)
.Join(db.Person, f => f.personId, p => p.personId, (f, p) =>
new // anonymous object
{
personId = p.personId,
addressId = p.addressId,
favoriteId = f.favoriteId,
})
.AsEnumerable() // database query ends here, the rest is a query in memory
.Select(x =>
new PersonDTO
{
personId = x.personId,
addressId = x.addressId,
favoriteId = x.favoriteId,
personProtocol = ppCombined
.Where(p => p.personId == x.personId)
.Select(p => new PersonProtocol
{
personProtocolId = p.personProtocolId,
activateDt = p.activateDt,
personId = p.personId
})
.ToList()
});

Unable to create a constant value of type ' ' . Only primitive types or enumeration types are supported in this context

I am not sure you are using EF, if you are then

If you have a Primarykey in the Route table then use that to compare objects. like

var query =
from r in db.Routes
join v in visitList on r.Id equals v.Route.Id into visits
select new { RouteObject = r, VisitList = visits };


Related Topics



Leave a reply



Submit