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()
});
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();
LINQ, Unable to create a constant value of type XXX. Only primitive types or enumeration types are supported in this context
You can't use Contains
with non-primitive values. Do
Where(l => l.Courses.Select(c => c.CourseId).Contains(courseId)
(or the Id field you use).
Unable to create a constant value - only primitive types
First way :
Remove ToList()
in the first query.
Or
//instead of retrieving mathings List, retrieve only the productIds you need (which are a List of Primitive types)
var productIdList = db.matchings
.Where(m => m.StoreId == StoreId)
.Select(x => x.ProductId)
.ToList();
var products = db.Products
.Where(p => productIdList
.Contains(p.ProductId))
.ToList();
Or
//other way
var produts = db.Products
.Where(p => db.matchings
.Any(m => m.StoreId == StoreId &&
m.ProductId == p.ProductId)
)
.ToList();
Because I think you're in linq2entities, and you're using a List of Matchings in a query which is not possible (the title of your topic tend to make me believe that's your problem).
C# - Linq : Unable to create a constant value of type Only primitive types or enumeration types are supported in this context.
The exception message is quite descriptive.
DefaultIfEmpty(new Person_Login({Id = -1})
is not supported in Linq to Entities.
You can use the following instead
var login = context.Person_Login
.FirstOrDefault(c => c.Username == username && c.Password == password)
?? new Person_Login {Id = -1};
Note that DefaultIfEmpty
method is used mainly for performing LINQ left outer joins.
Linq Any - Only primitive types or enumeration types are supported in this context
Try:
if (medico.Especialidad.Count > 0)
{
var medicoId = medico.Especialidad.FirstOrDefault().ID
listaMedicos = FiltrarPor(listaMedicos,
x => x.Especialidad.Any(e => e.ID == medicoId));
}
actually, better, would be:
var medicoEspecialidad = medico.Especialidad.FirstOrDefault();
if (medicoEspecialidad != null)
{
var medicoId = medicoEspecialidad.ID
listaMedicos = FiltrarPor(listaMedicos, x => x.Especialidad.Any(e => e.ID == medicoId));
}
Unable to create a constant value of type (type) Only primitive types ('such as Int32, String, and Guid') are supported in this context
It will not work because you want to use local Album in linq-to-entities query. You must either use navigation property on p to get its album:
var query = from p in VisibleObjects.OfType<Photo>()
where p.Album.Id == alb.Id
select p;
or you must build complex query with some join between photos and albums. You cannot pass local object and any its relation to the query. Only simple properties can be passed.
Related Topics
How to Display a Windows Form in Full Screen on Top of the Taskbar
Companyname.Foo' Is a 'Namespace' But Is Used Like a 'Type'
Best Way to Store Encryption Keys in .Net C#
How to Ensure a Timestamp Is Always Unique
Sql: Update a Row and Returning a Column Value with 1 Query
Connecting to SQL Server with Visual Studio Express Editions
Best Way to Handle Integer Overflow in C#
Securing a Password in Source Code
Can a Dbcontext Enforce a Filter Policy
C# Speech Recognition - Is This What the User Said
How to Get Xml Node from Xdocument
How to Make My Windows Form App Snap to Screen Edges
Iterating Through the Alphabet - C# A-Caz
Login Using Google Oauth 2.0 with C#
How to Make Chrome Headless After I Login Manually
How to Split an Ienumerable into Two by a Boolean Criteria Without Two Queries
Bind Multiple Combobox to a Single List - Issue: When I Select an Item, All Combo Boxes Change