Linq woes - 需要CollectionType [英] Linq woes - A CollectionType is required

查看:303
本文介绍了Linq woes - 需要CollectionType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的linq查询不工作?



我怀疑它可能与延迟加载有关。


{
使用(var ctx = ObjectContextManager< MyDataContext> .GetManager(MyDataContext))
{
(GetDocumentPayments(int documentId,bool?allowRepeatPayments,byte [] paymentStatuses,int [] paymentMethods)
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 =
来自ctx.ObjectContext.Documents.OfType中的付款< Payment>()
其中
ps.Contains(payment.Status)&&
pm.Contains(payment.Method)&&
payment.DocumentId == documentId&&&
(allowRepeatPayments == null || payment.AllowRepeatPayments == allowRepeatPayments)
orderby payment.Id
选择付款;

foreach(数据中的var p)//此处失败
{
payments.Add(ReadData(p));
}

退款;
}
}

抛出错误:需要CollectionType。
参数名称:collectionType

解决方案

构建(allowRepeatPayments == null || payment.AllowRepeatPayments == allowRepeatPayments)可以做一个有趣的事情查询。尝试在您执行时会发生什么:

  if(allowRepeatPayments.HasValue)
{
data = data。 where(p => p.AllowRepeatPayments == allowRepeatPayments);
}

您也可以对 paymentStatuses paymentMethods



这可能会解决你的问题,但如果不是, ,因为条件只在有必要时添加,而SQL不是杂乱的。


Why doesn't my linq query work?

I suspect it may have something to do with lazy loading. It seems to work in 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;
        }
    }

Throws error: A CollectionType is required. Parameter name: collectionType.

解决方案

Constructs like (allowRepeatPayments == null || payment.AllowRepeatPayments == allowRepeatPayments) can do funny thing to a query. Try what happens when you do:

if (allowRepeatPayments.HasValue)
{
    data = data.Where(p => p.AllowRepeatPayments == allowRepeatPayments);
}

You can do the same for paymentStatuses and paymentMethods.

It may solve your problem, but if not, it is an improvement anyway, because the condition is only added when it is necessary and the SQL is not cluttered when it isn't.

这篇关于Linq woes - 需要CollectionType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆