2012-02-24 171 views
0

我试图添加一列到以下LINQ表达式。我想让列在一个名为WasteItems的很多表中包含字符串连接的文本值。加入将在“Waste.WasteId = WasteItem.WasteId”上。我的问题是我需要在一个动态列中显示一个字符串,例如“EW(5); EX(3)”,如果WasteItem中有8条记录,并且包含2个字符字符串的列被称为WasteItem.EWC。希望这是有道理的,因为我意识到LINQ是非常强大的,所以必须有一个有效的方法。我是新来的,并不能确定如何启动或去这个问题:LINQTOSQL需要帮助

return from waste in this._db.Wastes 
      where (from u in _db.UsersToSites.Where(p => p.UserId == userId && p.SystemTypeId == SystemType.W) 
        select u.SiteId) 
        .Contains(waste.SiteId) 
      orderby waste.Entered descending select waste; 

在此先感谢

回答

0

像这样的东西应该做的:

wastes.GroupJoin(db.WasteItems, w => w.WastId, wi => wi.WasteId, (w,wi) => new { w, wi }) 
    .AsEnumerable() 
.Select(x => new 
{ 
    x.w.Name, 
    Items = string.Join(", ", x.wi.GroupBy(wi => wi.EWC).Select(g => string.Format("{0} ({1})", g.Key, g.Count()))) 
}) 

哪里wastes从结果您的查询。 AsEnumerable()是必要的,因为实体框架无法处理string.Join,因此必须在内存中处理该部分。

显然,我无法检查语法,但至少它可能会告诉你要走的路。