LINQ等效于string.Join,可在LINQ to Entities中使用 [英] LINQ equivalent of string.Join that is usable in LINQ to Entities

查看:111
本文介绍了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)
});

这篇关于LINQ等效于string.Join,可在LINQ to Entities中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆