and operator in Linq and select distinct values using linq

雨燕双飞 提交于 2020-01-06 19:37:14

问题


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


回答1:


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());



回答2:


It looks like you are passing 2 lambdas to the Where function and trying to logical-and (&&) them together. You can't do that. The && has to occur inside the Where lambda. Or you can chain 2 Where functions together. 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).



来源:https://stackoverflow.com/questions/12768492/and-operator-in-linq-and-select-distinct-values-using-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!