I can't seem to figure out how to compare boolean values in a C# lambda expression for EF4. I've tried:
cl.Where(c => c.Received == false);
and this:
cl.Where(c => !c.Received);
and this:
cl.Where(c => c.Received.Equals(false));
but I keep getting this error:
Exception Details: System.NotSupportedException: Unable to create a constant value
of type 'System.Object'. Only primitive types ('such as Int32, String, and Guid')
are supported in this context.
After spending a good amount of time researching this I'm still missing something. I'm fairly new to Lambdas so pointers would be appreciated.
Edit2: more code re:comment
int bar = 42;
var cl = db.foo.Where(c => c.baz.Equals(bar));
//codez (just an if statement)
cl.Where(c => c.Received == false).OrderByDescending(c => c.dateAdded);
That's it. Even if I remove the orderby it still doesn't work
Edit3:
Solution:
int bar = 42;
var cl = db.foo.Where(c => c.baz == bar);
cl.Where(c => c.Received == false).OrderByDescending(c => c.dateAdded);
The issue is most likely in the c.baz.Equals(bar) line. If you change it to
var cl = db.foo.Where(c => c.baz.Equals(bar)).ToList();
you should see the exception thrown on that line, because you force evaluation of the IQueryable<T>.
Instead of comparing objects, you should compare their IDs, like this:
(edited to reflect the conversation in the comments and changes to the OP)
var cl = db.foo.Where(c => c.baz == bar.id);
What is the type of Received ? Where close need a predicate(a function that return Boolean) in order to act upon every element in the list and return those that satisfy the condition. From the error message it seems the Received in not primitive type. You need to cast it to boolean if you for sure know its boolean.
Related
Say I have IEnumOfFood that contains a food object that lists out each ingredient. So something like IEnumOfFood[0] would be {'Celery', 'Onion', 'Chicken', [Stock]}. In this case, [Stock] is another IEnumerable. I'm trying to get each element in IEnumOfFood where Stock.Herb == "Bay Leaf".
It seems that no matter how I try and reword the LINQ query it always has some sort of error. Currently I have this:
IEnumOfFood= IEnumFoodItems.Select(x => x.Select(
y => y.Stock.Where(
y => y.Herb == "BayLeaf")));
Which throws a 'Cannot convert lambda expression to type 'bool' because it is not a delegate type'.
This is just one of the many ways I've tried to get an element of IEnumOfFood where the Stock Enum has herb as 'Bay Leaf', I'm not saying this is what should work.
Have you tryed:
IEnumFoodItems.Where(x => x.Any(s => s.Stock.Any(h => h.Herb == "BayLeaf")))
This piece of code keep making this error. :
Unable to create a constant value of type 'Repository.DBModel.Subscriber'. Only primitive types or enumeration types are supported in this context.
I've Changed it a few times but it keeps coming up with this Error.
using (SubscriberDBHandler db = new SubscriberDBHandler())
{
IEnumerable <Subscriber> NewSubscribers = Subscribers
.Where(sub => db.Subscriber
.Any(aSub => !aSub.Email.Equals(sub.Email)));
List<Subscriber> updateSubscribers = db.Subscriber
.Where(dbSub => Subscribers
.Any(lSub => lSub.Email
.Equals(dbSub.Email))).ToList();
if(NewSubscribers.Count() >= 1)
{
db.Subscriber.AddRange(NewSubscribers);
}
updateSubscribers.ForEach(aSub => aSub.State = Subscribers
.FirstOrDefault(sub => sub.Email
.Equals(aSub.Email)).State ?? "Error" );
db.SaveChanges();
}
I'd greatly appreciate if someone could point out my error or come up with a more efficient way to do this.
In advance thanks for your time and help.
I know there are a few post with this error out there but when reading them I can't figure out how they relate to my problem. so I'm sorry if this is a common mistake and others have provided a solution
The object Subscribers is a List<Subscriber>
I don't seem to be able to find the line but. the stack trace does contain this.
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
at Repository.SubScribRepository.AddOrUpdateSubscribers(List1 Subscribers)
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();
Changing updateSubscribers to an IEnumerable will prevent this error
This piece of code keep making this error. :
Unable to create a constant value of type 'Repository.DBModel.Subscriber'. Only primitive types or enumeration types are supported in this context.
I've Changed it a few times but it keeps coming up with this Error.
using (SubscriberDBHandler db = new SubscriberDBHandler())
{
IEnumerable <Subscriber> NewSubscribers = Subscribers
.Where(sub => db.Subscriber
.Any(aSub => !aSub.Email.Equals(sub.Email)));
List<Subscriber> updateSubscribers = db.Subscriber
.Where(dbSub => Subscribers
.Any(lSub => lSub.Email
.Equals(dbSub.Email))).ToList();
if(NewSubscribers.Count() >= 1)
{
db.Subscriber.AddRange(NewSubscribers);
}
updateSubscribers.ForEach(aSub => aSub.State = Subscribers
.FirstOrDefault(sub => sub.Email
.Equals(aSub.Email)).State ?? "Error" );
db.SaveChanges();
}
I'd greatly appreciate if someone could point out my error or come up with a more efficient way to do this.
In advance thanks for your time and help.
I know there are a few post with this error out there but when reading them I can't figure out how they relate to my problem. so I'm sorry if this is a common mistake and others have provided a solution
The object Subscribers is a List<Subscriber>
I don't seem to be able to find the line but. the stack trace does contain this.
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
at Repository.SubScribRepository.AddOrUpdateSubscribers(List1 Subscribers)
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();
Changing updateSubscribers to an IEnumerable will prevent this error
Why does:
var allShapes = _context.AttributeValuesLibraries.Where(x => x.AttNameID.Equals(1)).Select(y => y);
work, but I get the error "Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context" when I exclude the Select() like:
var allShapes = _context.AttributeValuesLibraries.Where(x => x.AttNameID.Equals(1));
Is there another way to write this query so it makes more sense? I just played with the query to make it work.
Thanks in advance!
Try this:
var allShapes = _context.AttributeValuesLibraries.Where(x => x.AttNameID == 1);
It really depends on your Linq provider.
My entity NewsItem has a nullable foreign key property: LibraryID of type int?.
My issue is when I query the property and compare it with any value except null, I get exceptions.
Initially my code was:
int? lid = ...
var results = context.NewsItems
.Where(n => n.LibraryID == lid);
but it gives me no results at all, no matter what lid is.
So, I tried:
var results = context.NewsItems
.Where(n => n.LibraryID.Equals(lid));
gives exception:
Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.
and then I tried:
var results = context.NewsItems
.Where(n => lid.Equals(n.LibraryID));
and got:
Unable to cast the type 'System.Nullable`1' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.
and this:
var results = context.NewsItems
.Where(n => object.Equals(lid, n.LibraryID));
gives same exception as the last one.
Now I was desperate, so I tried to complicate stuff (like other forums suggested, for example here):
var results = context.NewsItems
.Where(n => (lid == null ? n.LibraryID == null : n.LibraryID == lid));
but still getting same exception.
So... any SIMPLE workarounds?
How about
var results = context.NewsItems
.Where(n => lid.HasValue ? lid.Value == n.LibraryId.Value : (!n.LibraryId.HasValue) );
Hmm, that first snippet should work. I've used nullables like that many times. First thing I'd do is a sanity check just to make sure LibraryID is really int? and not long? or similar.
Other than that, you can try this:
var results = context.NewsItems
.Where(n => (lid.HasValue ? n.LibraryID == lid.Value : !n.LibraryID.HasValue));
Or to avoid the ?: within the query:
var results = lid.HasValue
? context.NewsItems.Where(n => n.LibraryID == lid.Value)
: context.NewsItems.Where(n => !n.LibraryID.HasValue);
It seems that EF does not find the correct operator overload. Therefore it produces wrong results if you set lid = null.
Use linq to objects by adding AsEnumerable() to your query and everything is fine:
var results = context.NewsItems.AsEnumeryble().Where(n => n.LibraryID == lid);
According to the MSDN docs (which I finally found), .Where() will only filter your collection. If you want to see if there are actually results, resolve by lazily executing the filtered query with .ToList(), GetEnumerator, or enumerating the collection with foreach;
This method is implemented by using deferred execution. The immediate
return value is an object that stores all the information that is
required to perform the action. The query represented by this method
is not executed until the object is enumerated either by calling its
GetEnumerator method directly or by using foreach in Visual C# or For
Each in Visual Basic.
http://msdn.microsoft.com/en-us/library/bb534803.aspx
int? lid = ...
var results = context.NewsItems
.Where(n => n.LibraryID == lid).ToList();
var results = context.NewsItems
.Where(n => n.LibraryID.HasValue && n.LibraryID.Value == lid.Value );
edit:
Previous filter was based on my understanding that you wanted to filter to entires having a particular value. Updated will filter to null or value.
var results = context.NewsItems
.Where(n => !n.LibraryID.HasValue || n.LibraryID.Value == lid.Value );