当未使用EXISTS引入子查询时,只能在选择列表中指定一个表达式

时间:2011-05-09 11:26:41

标签: c# .net sql linq

这个LINQ语句出了什么问题:

(from deposit in DAOBase<CashBookDeposit, long>.GetIQueryableBusinessBase()
 where deposit.Id == id
 select new CashBookDeposit
 {
     Id = deposit.Id,

     DepositeNumber = deposit.DepositeNumber,
     DocumentTypeName = deposit.CashBookDepositDocumentType.EnglishName,
     StatusName = deposit.CashBookDepositeStatus.EnglishName,

     CashbookName = deposit.CashBook.EnglishName,
     Collector = deposit.Collector,
     Partner = deposit.Partner,
     CashBookDepositDocumentType = deposit.CashBookDepositDocumentType,
     CreationDate = deposit.CreationDate,

     PaidAmount = deposit.TotalAmount,
     Description = deposit.Description,

     TempReceipts = (from receipt in deposit.TempReceipts 
                     select new TempReceipt
                     {
                         Id = receipt.Id,
                         Code = receipt.Code,
                         ReceiptNo = receipt.ReceiptNo,
                         Amount = receipt.Amount,
                         BusinessPartnerName = receipt.Partner.ENName,
                         CollectorName = receipt.Collector.EnglishName,
                         StatusName = receipt.TempReceiptStatus.EnglishName,
                         CollectionDate = receipt.CollectionDate,
                         CreationDate = receipt.CreationDate,
                         Description = receipt.Description,
                     }).ToList()
 }).SingleOrDefault();

我遇到了这个例外:

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

需要帮助,谢谢

1 个答案:

答案 0 :(得分:-1)

您可以按如下方式更改查询:

(from deposit in DAOBase<CashBookDeposit, long>.GetIQueryableBusinessBase()
 let receipts = (from tempReceipts in deposit.TempReceipts
                 select tempReceipts)
 where deposit.Id == id
 select new CashBookDeposit
 {
     Id = deposit.Id,

     DepositeNumber = deposit.DepositeNumber,
     DocumentTypeName = deposit.CashBookDepositDocumentType.EnglishName,
     StatusName = deposit.CashBookDepositeStatus.EnglishName,

     CashbookName = deposit.CashBook.EnglishName,
     Collector = deposit.Collector,
     Partner = deposit.Partner,
     CashBookDepositDocumentType = deposit.CashBookDepositDocumentType,
     CreationDate = deposit.CreationDate,

     PaidAmount = deposit.TotalAmount,
     Description = deposit.Description,

     TempReceipts = (from receipt in receipts 
                     select new TempReceipt
                     {
                         Id = receipt.Id,
                         Code = receipt.Code,
                         ReceiptNo = receipt.ReceiptNo,
                         Amount = receipt.Amount,
                         BusinessPartnerName = receipt.Partner.ENName,
                         CollectorName = receipt.Collector.EnglishName,
                         StatusName = receipt.TempReceiptStatus.EnglishName,
                         CollectionDate = receipt.CollectionDate,
                         CreationDate = receipt.CreationDate,
                         Description = receipt.Description,
                     }).ToList()
 }).SingleOrDefault();