简体   繁体   English

从LiNQ查询中获取结果

[英]getting result from LiNQ query

I'm new to using LiNQ. 我是使用LiNQ的新手。 I have the following code, which is used to find the order quantity of a part on an invoice object. 我有以下代码,用于查找发票对象上零件的订购数量。

var invoiceQty = from i in returnInvoices
                 where i.PartNo == returnPart.PartNo && i.InvoiceID == hiddenInvoiceId.Value
                 select i.OrderLineQty;

if (Convert.ToInt32(txtReturnProdQty.Text) > Convert.ToInt32(invoiceQty))
{
    args.IsValid = false;
    SourceValidate.ErrorMessage = "Returned qty cannot be greater than quantity available on the invoice.";
    txtReturnProdQty.Focus();
    return;
}

I don't think I'm getting the OrderLineQty value correctly for the if statement, as it generates the following error: 我认为我无法正确获取if语句的OrderLineQty值,因为它会产生以下错误:

System.InvalidCastException: Unable to cast object of type  'WhereSelectListIterator`2[Invoice,System.Double]' to type 'System.IConvertible'.

Can anyone help me understand how to use the returned value in the LiNQ query? 谁能帮助我了解如何在LiNQ查询中使用返回值?

LiNQ is taking a while to sink in! LiNQ需要一段时间才能下沉!

A linq expression is not evaluated until "used". 直到“使用”,才会评估linq表达式。

This means ie calling invoiceQty.ToList() or .First() 这意味着调用invoiceQty.ToList()或.First()

Until then invoiceQty type is "an expression" and not the effective type. 在此之前,invoiceQty类型是“表达式”,而不是有效类型。 To get the total quantity you need: 要获得总数量,您需要:

invoiceQty.Sum()

or simply replace the query: 或简单地替换查询:

var invoiceQty = (from i in returnInvoices
                 where i.PartNo == returnPart.PartNo && i.InvoiceID == hiddenInvoiceId.Value
                 select i.OrderLineQty).Sum();

This is because you are returning an IEnumerable<T> , if OrderLineQty is an int, then invoiceQty is of type IEnumerable<int> . 这是因为您要返回IEnumerable<T> ,如果OrderLineQty是int,则invoiceQty的类型是IEnumerable<int>

This is meaningless when you do a comparison. 当您进行比较时,这是没有意义的。

If you expect only one result then use .Single() here 如果只期望一个结果,则在此处使用.Single()

Linq is like a SQL query, if you're familiar with that. 如果您熟悉,Linq就像一个SQL查询。 In your code, invoiceQty will contain a LIST (more specifically, an IQueryable) of i.OrderLineQty that meet your search criteria in the where clause. 在您的代码中,invoiceQty将包含满足您的where子句中的搜索条件的i.OrderLineQty的LIST(更具体地说是IQueryable)。 Even if there is only one that matches, it will still give you a list with one element. 即使只有一个匹配项,它仍然会为您提供一个元素列表。

You look to know for certain only one will match (and the where clause seems to support that assumption), so if your case you can request a Single, First, SingleOrDefault, or FirstOrDefault ( click here for a full list of methods available) 您可能会确定只有一个匹配(并且where子句似乎支持该假设),因此,如果您遇到这种情况,可以请求Single,First,SingleOrDefault或FirstOrDefault( 单击此处以获取可用方法的完整列表)

if (Convert.ToInt32(txtReturnProdQty.Text) > Convert.ToInt32(invoiceQty.First()))

Try this way: 尝试这种方式:

if (invoiceQty.FirstOrDefault() != null) return;

if (Convert.ToInt32(txtReturnProdQty.Text) > (decimal)invoiceQty.First())
{
    args.IsValid = false;
    SourceValidate.ErrorMessage = "Returned qty cannot be greater than quantity available on the invoice.";
    txtReturnProdQty.Focus();
    return;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM