views:

5043

answers:

7

I have a Linq to Entities query like this one:

var results = from r in entities.MachineRevision
              where r.Machine.IdMachine == pIdMachine
                 && r.Category == (int)pCategory
              select r;

Usually, I use the code below to check if some results are returned:

if (results.Count() > 0)
{
    return new oMachineRevision(results.First().IdMachineRevision);
}

However, I'm getting NotSupportedException in the if condition.

The error message is: Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.

Note that pCategory is an Enum type.

A: 

try using

IENumerable<MachineRevision> results = from r in entities.MachineRevision
...

instead.

I think its the var that's causing your issue.

DeletedAccount
I had already tried, and the result is the same :-(
Nelson Reis
+1  A: 

Why not just use FirstOrDefault() instead, and check for null? I can't see the benefit in querying for the count and then taking the first element.

Jon Skeet
I can see your point. However, if I use FirstOrDefault(), I'm still getting that NotSupportedException, because I'm trying to execute that method on an entity object (that's what is returned by the Linq statement), not on a IEnumerable.
Nelson Reis
+8  A: 

EDIT: Based on your update, the error may be related to an enum in your entity class. See this blog entry for more information and a work-around. I'm leaving my original answer as an improvement on your query syntax.

Try doing the selection of the first entity in the query itself using FirstOrDefault and then check if the result is null.

int compareCategory = (int)pCategory; // just a guess
var result = (from r in entities.MachineRevision
              where r.Machine.IdMachine == pIdMachine
                 && r.Category == compareCategory
              select r).FirstOrDefault();

if (result != null)
{
     return new oMachineRevision(result.IdMachineRevision);
}
tvanfosson
Still doesn't work, because we're trying to execute the FirstOrDefault() method on an entity object (that's what is returned by the linq statement) and not an IEnumerable.
Nelson Reis
Select always returns an IEnumerable, see docs here http://msdn.microsoft.com/en-us/library/bb548891.aspx. Are you sure that the example is exactly the same as your code?
tvanfosson
My mistake, I apologize for that. To simplify the code, I've removed an enum, just like tvanfosson noticed by the error message I was getting. Thanks to all, and sorry for misleading you.
Nelson Reis
+ reference to enum helped me. thanks.
Sky Sanders
A: 

Edit:

Read the error message. "Unable to create a constant value of type 'Closure type'. Only primitive types ('such as Int32, String, and Guid') are supported in this context."

One of these comparisions is with a type that is not int, string or guid. I'm guessing the Category.

r.Machine.IdMachine == pIdMachine && r.Category == pCategory

Interestingly, LinqToSql will allow this construction. Don't know why LinqToEntities does not support this.

David B
+1  A: 

I didn't know different anonymous objects would be created depending on the query result. I guess they just wanted results to be of type IEnumerable

How about using a foreach?

var results = from r in entities.MachineRevision
              where r.Machine.IdMachine == pIdMachine
                 && r.Category == pCategory
              select r;

foreach( var r in results )
{
    yield return new oMachineRevision( r.IdMachineRevision );
}
Trap
+1  A: 

In the standard implementation of linq, the operators "select" and "where" map to methods that return an IEnumerable or IQueryable. So standard linq methods when used should always return an IEnumerable from your query not a single object.

But linq methods that are candidates for the linq operators are not restricted to methods returning IEnumerables, any method returning anything can be chosen.

In case you have instance methods named "Select" and "Where" that return a single object or extensions methods that are specific to your class and return a single object those will be used instead of the standard linq ones.

My guess is that either a "Select" or "Where" method defined in your class is making linq return a single value instead of a IEnumerable<T>.

Pop Catalin
A: 

I think you could also select the item you want another, simpler way by using lamda expressions.

var result = entities.MachineRevision
                 .Where(x => x.Machine.IdMachine == pIdMachine)
                 .Where(y => y.Category == (int)pCategory)
                 .FirstOrDefault();

if (result != null)
{
     return new oMachineRevision(result.IdMachineRevision);
}

and then proceeding as you would normally

danrichardson
Doesn't work, because the ".Where(y => y.Category == (int)pCategory)" produces an error. You need to do the cast of the Enum before the query.var iCategory = (int)pCategory;and then just use the iCategory in the query itself.
derSteve