String 如何进行linq到sql字符串连接?
我想将列表的名称合并到列表中。 错误消息:LINQ to Entities无法识别方法“System.String Join(System.String,System.String[])”方法,并且无法将此方法转换为存储表达式String 如何进行linq到sql字符串连接?,string,linq,join,linq-to-sql,String,Linq,Join,Linq To Sql,我想将列表的名称合并到列表中。 错误消息:LINQ to Entities无法识别方法“System.String Join(System.String,System.String[])”方法,并且无法将此方法转换为存储表达式 dgv.DataSource = this.oFacade.Context.Books .Include(c => c.Categories)
dgv.DataSource = this.oFacade.Context.Books
.Include(c => c.Categories)
.Select(c => new dtoObjectDGV
{
ID = c.ID,
Name = c.Name,
CategoryInfo= String.Join(", ", c.Categories.Select(d => d.Name).ToArray())
}).ToList();
结果:名称=C#图书类别信息=X类别,Y类别,。。。bla bla
Join
方法在Linq to Entities中不受支持,我建议您首先投影所需的信息,然后使用AsEnumerable
扩展方法,您可以切换到Linq to Object以使用Join
方法:
DataSource=this.oFacade.Context.Books
.Include(c => c.Categories)
.Select(c => new
{
ID = c.ID,
Name = c.Name,
Categories= c.Categories.Select(d => d.Name)
})
.AsEnumerable()
.Select(c => new dtoObjectDGV
{
ID = c.ID,
Name = c.Name,
CategoryInfo= String.Join(", ", c.Categories)
})
.ToList();
如果你的表不需要很多列,那么你可以考虑不要做第一个投影:
DataSource=this.oFacade.Context.Books
.Include(c => c.Categories)
.AsEnumerable()
.Select(c => new dtoObjectDGV
{
ID = c.ID,
Name = c.Name,
CategoryInfo= String.Join(", ", c.Categories.Select(d => d.Name).ToArray())
})
.ToList();