和运算符,并使用linq选择不同的值 [英] and operator in Linq and select distinct values using linq

查看:187
本文介绍了和运算符,并使用linq选择不同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.net的新人。我有一个形式,其中有两个组合框 cbProduct cbBrandName 以及标签 lblPrice



我试图实现下面的代码,但它显示蓝色涂鸦&&
(错误:运算符&&'不能应用于类型为'lambda expression'和'lambda expression'的操作数



我试过下面的代码:



pre pre> lblPrice.Text = string。空;
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
)))。选择(k => k.Element(price
).Value).ToString();

我的另一个问题是,我想使所选的值 cbProduct 。以下代码使用所有值,而不是不同的值:

  cbProduct.Items.AddRange(doc.Descendants(items
).Select(x => x.Element(productname)。Value
).ToArray< string>()); //添加所有产品
cbProduct.SelectedIndex = 0;

给任何一个答案都是好的



请帮助我

提前感谢

解决方案

对于第一个问题,看起来你只是想选择一个价格。这个代码将工作,假设该项由 .Single()找到。否则会抛出,在这种情况下,你应该使用 .SingleOrDefault()并在找到的项目上检查null。

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

对于第二个问题,您需要关闭 .Select ,使用括号,然后可以调用 .Distinct() .ToArray()以区分并将结果投影到 string [] 。我还在那里抛出一个 .OrderBy(),因为没有什么比随机顺序的ComboBox更烦人了。尝试这样:

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


I am new to .net. I have a form in which there are two comboboxes cbProduct and cbBrandName and also a label 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')

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. 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

解决方案

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(). It will throw otherwise, in which case you should use .SingleOrDefault() and check for null on the found item.

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[]. I've also thrown an .OrderBy() in there, as there's nothing more annoying than a ComboBox in a random order. Try this:

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

这篇关于和运算符,并使用linq选择不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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