【发布时间】:2015-12-04 12:17:40
【问题描述】:
this Stack Overflow question 帮助我。
现在我正在构建查询,需要向其他表添加连接以引入名称和描述等。
我的查询是这样的:
using (var ctx = new myEntities())
{
var pc = ctx.tblPostcodes.Where(z => z.Postcode == postcodeOutward)
.Select(x => new {postcodeId = x.PostcodeID}).Single();
int pcId = pc.postcodeId;
var q = ctx.tblPrices.OrderByDescending(x => x.Cost)
.Where(c => c.PostcodeID == pcId)
.Where(s => s.ItemID < 18)
.GroupBy(x => x.ItemID)
.Select(g => new { g, count = g.Count() })
.ToList()
.SelectMany(t => t.g.Select(b => b).Zip(Enumerable.Range(1, t.count), (j, i) => new { j.ItemID, j.Cost, j.SupplierID }));
foreach (var i in q)
{
sb.AppendFormat("ItemId = {0}, Cost = {1}, SupplierId = {2}<hr/>", i.ItemID, i.Cost, i.SupplierID);
}
}
我正在尝试添加以下联接:
.Join(ctx.tblItems, it => it.ItemID, s => s.ItemID, (it, s) => new { it, s })
但它会导致模棱两可的调用错误。有任何想法吗?我需要再添加两个内部连接。我希望能做对一个,另外两个会很容易(希望如此)。
【问题讨论】:
-
你先用数据库?
-
是的!我必须将其作为当前使用的。
-
根据您的模型,您应该能够(在存储过程中)做“疯狂”的事情,例如:
x.Address.PrimaryOwner.Name,Linq 将在后台处理所有必需的连接。表达输出,而不是你习惯的方式。 -
新 { j.ItemID,j.Cost,j.SupplierID,j.tblItems.Name}。很酷...