0

I have a class called Person that has a method called hasPermission(Int 32)

I'm trying to call some linq and have tried various options but haven't managed to find a solution yet if anyone has any ideas, please can you help.

I currently have

IEnumerable<Person> co = this.Person();

//this.Person().Where(o => o.IsDeleted == false);

//    src.Where(o => o.IsDeleted == false).Select(v => new { Text = v.Name });
return this.Json(co,JsonRequestBehavior.AllowGet);

you can see a couple of the other things i have tried in the comments.

Thanks for your help in advance

Gemma

5
  • 3
    So, Person is a method that returns an IEnumerable<Person>? According to its name, it probably isn't. The Person method's signature would be helpful. Mar 9, 2012 at 12:30
  • 2
    What is the role of hasPermission here? You're looking for a solution, but don't clearly state the problem. Mar 9, 2012 at 12:39
  • yes the person is IEnumerable. The person class has a method for hasPermission(int permissionID) which is used to set the permissions of the person. However all I want to do is return a list of names from person. The problem is being caused by the LINQ to Entities method
    – gadgetgem
    Mar 9, 2012 at 12:45
  • error as below: context.Exception = {"LINQ to Entities does not recognize the method 'Boolean HasPermission(Int32)' method, and this method cannot be translated into a store expression."}
    – gadgetgem
    Mar 9, 2012 at 12:53
  • I figured that, but why didn't you post that to begin with? (like this.Person().Where(p => p.HasPermission(2)). Anyway, as long as you're operating on IQueryable everything must be translatable to SQL. That does not work with a common instance method. Mar 9, 2012 at 13:18

1 Answer 1

1

OK, as this is LINQ to Entities, you need to use AsEnumerable method:

// this.Person().Where(o => !o.IsDeleted) is translated by EF, the rest isn't
this.Person().Where(o => !o.IsDeleted).AsEnumerable().Where(p => p.hasPermission(5));

The AsEnumerable extension method wraps your LINQ to Entities result into a generic IEnumerable. Thus, subsequent LINQ isn't translated to SQL by EF, but just executed.

See MSDN for further information on AsEnumerable too: http://msdn.microsoft.com/en-us/library/bb335435.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.