LINQ等效于string.Join,可在LINQ to Entities中使用

LINQ等效于string.Join,可在LINQ to Entities中使用

问题描述:

我有一个带有一些自定义属性的元数据类,以便于数据访问.我遇到的一个问题是,当我尝试在LINQ语句中使用此属性时,会出现此错误.

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.

我知道为什么得到它,我想知道是否还有另一种方法可以使该属性在LINQ语句中使用.

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.

这里是属性:

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));

从数据库中选择所需的信息后,您可以执行string.Join .如果执行ToList(),这将从数据库中枚举所需的数据.然后,您可以执行所需的任何C#,因为它会在ToList()

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)
});