问题
I have a metadata class with some custom properties for easier data access. One problem that I have run across though, is that when I try to use this property in LINQ statements I get this error.
The specified type member 'CrewCodesStr' is not supported in LINQ to Entities.
I know why I get it, I'm wondering if there is another way to do this to allow the property to be used in LINQ statements.
Here is the property:
public string CrewCodesStr
{
get { return string.Join(", ", CrewCodesList); }
}
Here is an example query that it fails on.
query = query.Where(wo => searchCriteria.crewCode.Contains(wo.CrewCodesStr));
回答1:
You can do a string.Join after you've selected the info you need from your database. If you do a ToList(), this will enumerate the data you need from the database. Then you can do whatever C# you want, as it will run in memory after the ToList()
var result = MyObjects.Select(x => new
{
ID = x.ID,
CrewCodes = x.CrewCodes
})
.AsEnumerable()//enumerate the queryable
.Select(x => new
{
ID = x.ID,
String = string.Join(",",x.CrewCodes)
});
来源:https://stackoverflow.com/questions/33399272/linq-equivalent-of-string-join-that-is-usable-in-linq-to-entities