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
The Remote Server Returned an Unexpected Response: (413) Request Entity Too Large
C# Open File, Path Starting with %Userprofile%
Why Is 16 Byte the Recommended Size for Struct in C#
Sqlite .Net Performance, How to Speed Up Things
C# Httpwebrequest the Underlying Connection Was Closed: an Unexpected Error Occurred on a Send
How to Sync the Scrolling of Two Multiline Textboxes
Combine Two Linq Lambda Expressions
How to Disable Aero Snap in an Application
Cannot Deserialize the Current JSON Array (E.G. [1,2,3])
Exclude a Field/Property from the Database with Entity Framework 4 & Code-First
Process.Start to Open an Url, Getting an Exception
Does Foreach Automatically Call Dispose
Is the Sorting Algorithm Used by .Net's 'Array.Sort()' Method a Stable Algorithm
Open File in Exclusive Mode in C#
Using Extension Methods in .Net 2.0
Moq + Unit Testing - System.Reflection.Targetparametercountexception: Parameter Count Mismatch