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();
EF - Unable to create a constant value of type Only primitive types or enumeration types are supported in this context
When I run it this way:
dbc.PickupLocations.Where(pl => pl.ShippingProfileCountryID != 796 && !activeLockers.Select(al => al.LockerId).ToList().Contains(pl.ExternalID))
.ToList()
.ForEach(x => x.Status = 0);
dbc.SaveChanges();
I get this error:
Unable to create a constant value of type Only primitive types or
enumeration types are supported in this context
But like this it works:
var activeLockersIDs = activeLockers.Select(al => al.LockerId).ToList();
dbc.PickupLocations.Where(pl => pl.ShippingProfileCountryID != 796 && !activeLockersIDs.Contains(pl.ExternalID))
.ToList()
.ForEach(x => x.Status = 0);
dbc.SaveChanges();
I read about this error here:
Unable to create a constant value of type Only primitive types or enumeration types are supported in this context
But I am still not sure what was the exact problem, if someone understands and can explain in the comments it would be good to know.
Unable to create a constant value of type 'x'. Only primitive types or enumeration types are supported in this context
public List<VisaCodesNamesDto> GetVisaCodesNames(List<VisaCodesNamesInputDto>input)
{
List<string> filterlist = input.Select(x => x.VisaId.ToString() + "-" + x.VersionNo).ToList();
var result = (from visaConfigurationDocuments in Context.VisaConfigurationDocuments
join visaCodes in Context.VisaCodes
on visaConfigurationDocuments.VisaCodeId equals visaCodes.VisaCodeId
where filterlist.Any(x => x == visaConfigurationDocuments.VisaId.ToString() + "-" + visaConfigurationDocuments.VersionNo)
select new VisaCodesNamesDto
{
VisaCodeNameAr = visaCodes.NameAr,
VisaCodeNameEn = visaCodes.NameEn,
VisaId = visaConfigurationDocuments.VisaId,
VersionNo = visaConfigurationDocuments.VersionNo
}).ToList();
return result;}
i make a work around solution that solve my issue in converting type of complex object in where clause
Error: Only primitive types or enumeration types are supported in this context EF
You use a local collection, Subscribers
, directly in a LINQ statement. But these objects can't be translated into SQL. There are only mappings from primitive types to database types.
I'd suggest you use
var emails = Subscribers.Select(s => s.Email).ToList();
And proceed by using these strings (i.e. primitive values) in Contains
statements like:
var newSubscribers = db.Subscriber
.Where(dbSub => !emails.Contains(dbSub.Email))
.ToList();
var updateSubscribers = db.Subscriber
.Where(dbSub => emails.Contains(dbSub.Email))
.ToList();
Related Topics
Remove Characters from C# String
C# - Multiple Generic Types in One List
How to Properly Exit a C# Application
Cross-Thread Operation Not Valid
Generating an Xml Serialization Assembly as Part of My Build
Differencebetween Nullable<T>.Hasvalue or Nullable<T> != Null
Passing Properties by Reference in C#
How to Deserialize JSON into Ienumerable<Basetype> with Newtonsoft JSON.Net
C# - Faster Alternatives to Setpixel and Getpixel for Bitmaps for Windows Forms App
How to Stop C# Console Applications from Closing Automatically
How to Represent 0.1 in Floating Point Arithmetic and Decimal
How to Check the .Net Framework Version