0

我有一个定义如下的类:

public class SearchResult
{
    public Customer Customer { get; set; }
    public Receipt Receipt { get; set; }
    public IEnumerable<ReceiptDetail> ReceiptDetail { get; set; }
}

在我的 Linq 查询中,我想返回单个 SearchResult 记录,但在返回 ReceiptDetail 列表时遇到问题。如果我做:

 var list=(from cust in dbContext.Customers 
     join rec in dbContext.Receipts on cust.IdCustomer equals rec.IdCustomer
     join recdet in dbContext.ReceiptDetails on rec.IdReceipt equals recdet.IdReceipt
     select new SearchResult
     {
          Customer=cust,
          Receipt=rec,
          ReceiptDetail = (from  r1 in recdet select r1).ToList() // COMPILER ERROR HERE
     }).ToList();

我得到“找不到源类型 ReceiptDetail 'Select' 的查询模式的实现”。

我究竟做错了什么?

4

1 回答 1

1

嗯,recdet来自这里:

join recdet in dbContext.ReceiptDetails on rec.IdReceipt equals recdet.IdReceipt

这意味着它是一个单一的收据细节。然后,您尝试在此处将其用作集合

from  r1 in recdet select r1

我怀疑您实际上想要一个群组加入:

join recdet in dbContext.ReceiptDetails on rec.IdReceipt equals recdet.IdReceipt
  into recdets
...
ReceiptDetail = (from  r1 in recdets select r1).ToList()

或更简单的后半部分:

ReceiptDetail = recdets.ToList()
于 2012-10-02T21:44:41.827 回答