LINQ equivalent of string.Join that is usable in LINQ to Entities

泄露秘密 提交于 2020-01-17 05:01:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!