Cris
Cris

Reputation: 12194

Linq error when returning a List inside a List

I have a class defined as follows:

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

In my Linq query i would like to return a single SearchResult record but have problems in returning the List of ReceiptDetail. If i do:

 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();

I get the "Could not find an implementation of the query pattern for source type ReceiptDetail 'Select' not found".

What am i doing wrong?

Upvotes: 0

Views: 104

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500365

Well, recdet comes from here:

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

That means it's a single receipt detail. You're then trying to use it as a collection here:

from  r1 in recdet select r1

I suspect you actually want a group join:

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

or more simply for the latter part:

ReceiptDetail = recdets.ToList()

Upvotes: 1

Related Questions