简体   繁体   English

和Linq中的运算符,并使用linq选择不同的值

[英]and operator in Linq and select distinct values using linq

I am new to .net. 我是.net的新手。 I have a form in which there are two comboboxes cbProduct and cbBrandName and also a label lblPrice . 我有一种形式,其中有两个组合框cbProductcbBrandName以及标签lblPrice

I am trying to implement the below code but it is showing blue scribbles to && . 我正在尝试实现以下代码,但它向&&显示了蓝色涂鸦。 (Error: operator '&&' cannot be applied to operands of type 'lambda expression' and 'lambda expression' ) (错误: 运算符“ &&”不能应用于类型为“ lambda expression”和“ lambda expression”的操作数

I tried the below code: ( not working ) 我尝试了以下代码:( 不起作用

lblPrice.Text = string.Empty;
        lblPrice.Text = doc.Descendants("items"
            ).Where((x => x.Element("productname"
                ).Value.Equals(cbProduct.SelectedItem.ToString())) && /*blue scribbles to '&&'*/
                (y => y.Element("brandname").Value.Equals(cbBrandName.SelectedItem.ToString()
                ))).Select(k => k.Element("price"
                    ).Value).ToString();

My other question is that i want to make the selected values of cbProduct as distinct. 我的另一个问题是,我想使选定的cbProduct值与众不同。 The below code takes all the values instead of distinct values: 以下代码采用所有值,而不是不同的值:

cbProduct.Items.AddRange(doc.Descendants("items"
            ).Select(x => x.Element("productname").Value
            ).ToArray<string>());//adds all products           
        cbProduct.SelectedIndex = 0;

giving any one answer is ok 给出任何一个答案都可以

Please assist me 请帮我
Thanks in advance 提前致谢

It looks like you are passing 2 lambdas to the Where function and trying to logical-and (&&) them together. 看来您要将2个lambda传递给Where函数,并试图将它们逻辑与(&&)在一起。 You can't do that. 你不能那样做。 The && has to occur inside the Where lambda. &&必须出现在Where lambda内部。 Or you can chain 2 Where functions together. 或者,您可以将2 Where函数链接在一起。 Something like this: 像这样:

    lblPrice.Text = doc.Descendants("items")
                        .Where(x => x.Element("productname").Value.Equals(cbProduct.SelectedItem.ToString()) &&
                                    x.Element("brandname").Value.Equals(cbBrandName.SelectedItem.ToString()))
                        .Select(k => k.Element("price").Value).ToString();

The other issue I see is you are ending your query with a select, but never actually enumerating it. 我看到的另一个问题是,您将以选择结束查询,但从未真正枚举它。 You probably want to do something like this: 您可能想要执行以下操作:

lblPrice.Text = doc.Descendants("items")
                        .Where(x => x.Element("productname").Value.Equals(cbProduct.SelectedItem.ToString()) &&
                                    x.Element("brandname").Value.Equals(cbBrandName.SelectedItem.ToString()))
                        .Select(k => k.Element("price").Value)
                        .FirstOrDefault();

Which will return the string you are looking for, or null if nothing exists (so you probably want to skip the final .ToString() call in this case, since you are already returning a string from Select and .ToString() on a null will throw an exception). 它将返回您要查找的字符串,如果不存在则返回null(因此,在这种情况下,您可能想跳过最终的.ToString()调用,因为您已经从Select和.ToString()返回了一个null字符串会引发异常)。

For the first question, it looks like you just want to select the one price. 对于第一个问题,您似乎只想选择一个价格。 This code will work, assuming that the item is found by the .Single() . 假设该项目由.Single()找到,则此代码将起作用。 It will throw otherwise, in which case you should use .SingleOrDefault() and check for null on the found item. 否则将抛出该.SingleOrDefault() ,在这种情况下,应使用.SingleOrDefault()并检查找到的项目是否为null。

lblPrice.Text =
    doc.Descendants("items")
       .Single(x => x.Element("productname").Value == cbProduct.SelectedItem.ToString() &&
                    x.Element("brandname").Value == cbBrandName.SelectedItem.ToString())
       .Element("price").Value;

For the second question, you need to close off your .Select with a bracket, then you can call .Distinct() and .ToArray() to filter to distincts and project the result to string[] . 对于第二个问题,您需要使用方括号将.Select结束,然后可以调用.Distinct().ToArray()来过滤不同string[]并将结果投影到string[] I've also thrown an .OrderBy() in there, as there's nothing more annoying than a ComboBox in a random order. 我还在那里抛出了一个.OrderBy() ,因为没有什么比按随机顺序的ComboBox更令人讨厌了。 Try this: 尝试这个:

cbProduct.Items.AddRange(doc.Descendants("items")
                            .Select(item => item.Element("productname").Value)
                            .Distinct()
                            .OrderBy(item => item)
                            .ToArray());

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

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