LINQ to Entity: using Contains in the "select" portion throws unexpected error

Posted by Chu on Stack Overflow See other posts from Stack Overflow or by Chu
Published on 2009-04-29T12:37:11Z Indexed on 2010/04/05 18:43 UTC
Read the original article Hit count: 225

I've got a LINQ query going against an Entity Framework object. Here's a summary of the query:

//a list of my allies
List<int> allianceMembers = new List<int>() { 1,5,10 };

//query for fleets in my area, including any allies (and mark them as such)
var fleets = from af in FleetSource
             select new Fleet 
             {
                 fleetID = af.fleetID,
                 fleetName = af.fleetName,
                 isAllied = (allianceMembers.Contains(af.userID) ? true : false)
             };

Basically, what I'm doing is getting a set of fleets. The allianceMembers list contains INTs of all users who are allied with me. I want to set isAllied = true if the fleet's owner is part of that list, and false otherwise.

When I do this, I am seeing an exception: "LINQ to Entities does not recognize the method 'Boolean Contains(Int32)' method"

I can understand getting this error if I had used the contains in the where portion of the query, but why would I get it in the select? By this point I would assume the query would have executed and returned the results. This little ditty of code does nothing to constrain my data at all.

Any tips on how else I can accomplish what I need to with setting the isAllied flag?

Thanks

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about c#