为什么我的 linq 查询不起作用?
我怀疑这可能与延迟加载有关。它似乎在 linqpad 中工作。
public IList<PaymentDto> GetDocumentPayments(int documentId, bool? allowRepeatPayments, byte[] paymentStatuses, int[] paymentMethods)
{
using (var ctx = ObjectContextManager<MyDataContext>.GetManager("MyDataContext"))
{
var payments = new List<PaymentDto>();
var ps = new List<byte>();
if (paymentStatuses != null)
{
ps = paymentStatuses.ToList();
}
var pm = new List<int>();
if (paymentMethods != null)
{
pm = paymentMethods.ToList();
}
IQueryable<Payment> data =
from payment in ctx.ObjectContext.Documents.OfType<Payment>()
where
ps.Contains(payment.Status) &&
pm.Contains(payment.Method) &&
payment.DocumentId == documentId &&
(allowRepeatPayments == null || payment.AllowRepeatPayments == allowRepeatPayments)
orderby payment.Id
select payment;
foreach (var p in data) // Fails here
{
payments.Add(ReadData(p));
}
return payments;
}
}
引发错误:需要 CollectionType。参数名称:collectionType。