Only primitive types or enumeration types are supported in this context
Try this change that does not use a collection of Shift
s in the query:
public int saleCount(List<Shift> shifts) {
var shiftIds = shifts.Select(s => s.ID).ToList();
var query = (
from x in database.ItemSales
where shiftIds.Contains(x.shiftID)
select x.SalesCount
);
return query.Sum();
}
The idea is to avoid passing Shift
objects to the query provider, using IDs instead.
Only primitive types or enumeration types are supported in this context
My guess is that error indicates that EF cannot translate the equality operator for Employee
to SQL (regardless of whether you're assuming referential equality or an overridden ==
operator). Assuming the Employee
class has a unique identifier try:
var query = from a in db.Activities
where a.AssignedEmployeeId == currentUser.Id
where a.IsComplete == false
orderby a.DueDate
select a;
return View(query.ToList());
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();
Linq To Entities 'Only primitive types or enumeration types are supported' Error
basically it means you are using some complex datatype inside the query for comparison.
in your case i suspect from p in this.Plans where p.PlanID == pd.PlanID
is the culprit.
And it depends on DataProvider. It might work for Sql Data Provider, but not for SqlCE data Provider and so on.
what you should do is to convert your this.Plans
collection into a primitive type collection containing only the Ids i.e.
var integers = PlanDetails.Plans.Select(s=>s.Id).ToList();
and then use this list inside.
var q = from pd in PlanDetails
select new {
pd.PlanDetailID,
ThePlanName = (from p in integers
where p == pd.PlanID
select pd.PlanName)
};
C# Entity Framework Only primitive types or enumeration types are supported in this context
Entity framework has no idea what to do with your myAuthorVariable
reference, i.e. how would it translate that to SQL? SQL has no concept of references.
Basically, what you would want to do in this situation is give it a primitive type it can translate into SQL Like an Id
using (WFCDbContext db = new WFCDbContext())
{
List<Books> = db.Books.Where(x => x.Author.Id == myAuthorVariable.Id).toList()
}
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));
}
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 with expect: Only primitive types or enumeration types are supported in this context
The problem is your object nlnames
can't be translated into something which SQL Server understands. In order to get around this you could call .ToList()
before the .Except()
var filternamesdblist = filternamesdb
.Select(n => new FilterNameDto
{
ProductName = n.ProductName,
})
.ToList()
.Where(n => !nlnames.Any(nl => nl.ProductName == n.ProductName))
.ToList();
Alternatively, you could change the type of nlnames
to List<string>
which can be understood by SQL Server:
var nlnames = new List<string> { "Farouche", "La Parisienne" };
var filternamesdblist = filternamesdb
.Where(n => !nlnames.Contains(n.ProductName))
.Select(n => new FilterNameDto
{
ProductName = n.ProductName,
})
.ToList();
Only primitive types or enumeration types are supported in this context C# Link
Did you know that a C# char is 16 bit? So, not the same as c++ for example..
You should use: (similar code)
string.Join("",Context.Table1.Where(c => c.Column1== Value1)
.Select(c => c.Column2.Replace("~", "~" + "\r" + "\n").ToList());
Related Topics
How to Generate Random Color Names in C#
JSON.Net (De)Serialize Untyped Property
Drawing on Top of Controls Inside a Panel (C# Winforms)
Open a Folder Using Process.Start
Parallel.For(): Update Variable Outside of Loop
Difference Between the System.Array.Copyto() and System.Array.Clone()
Transparent Window Layer That Is Click-Through and Always Stays on Top
Convert Array of Strings to List<String>
Is Graphics.Drawimage Too Slow for Bigger Images
Get Iprincipal from Oauth Bearer Token in Owin
What Are the Differences Between the Xmlserializer and Binaryformatter
Why "Decimal" Is Not a Valid Attribute Parameter Type
Datetime.Adddays() Not Working as Expected
The Imported Project "C:\Microsoft.Csharp.Targets" Was Not Found
How to Define Properties in Partial Classes, Then Mark Them with Attributes in Another Partial Class