I am a .net beginner. I am reading a XML file and showing it in two comboboxes ie., cbProduct
and cbBrandName
I need to show text in cbBr
LINQ looks much more scary than it is. There's two bits of it being used in Anirudha's answer, which I'll try to explain. The first is .Select(x=>
. This means "For each thing in the list, replace it with something". The x represents each item in the list.
For example:
new string[]{"a", "b", "c"}.Select(x=>x.ToUpper());
turns an array of {"a", "b", "c"}, into an array of {"A", "B", "C"}. It's just saying "Take the each thing in the list, and replace it with whatever you get by calling ToUpper()
on it.
The other bit of LINQ is .Where(x=>
. That just says "Give me a smaller list which only has things where this statement is true". So
new string[]{"a", "b", "c"}.Where(x=>x == "a");
will give you a list of {"a"}. Replacing x == "a"
with x != "b"
will give you a list of {"a", "c"}. So in the second bit of code, you're saying "before I do that thing where I replace each item with its productname, I want to filter out anything that doesn't match what I want to match. Then I transform what's left."
To apply these to the code example, I'll reformat the lines and comment them.
// To set the first combo box:
cbProduct.Items.AddRange( // Add everything we produce from this to the cbProduct list
doc.Descendants("items") // For each thing that represents an "items" tag and it's subtags
.Select(x=>x.Element("productname").Value) // Transform it by getting the "productname" element and reading it's Value.
.ToArray<string>()); // Then convert that into a string[].
// To set the second combo box:
string product2Search=cbProduct.SelectedItem.ToString();// get the currently selected value of cbProduct.
cbBrandName.Items.Clear(); //clears all items in cbBrandNamedoc
cbBrandName.Items.AddRange( // Add everything we produce from this to the cbBrandName list
doc.Descendants("items") // For each thing that represents an "items" tag and it's subtags
.Where(x=>x.Element("productname").Value==product2Search) // Filter our list to only those things where the productname matches what's currently selected in cbProduct (which we just stored)
.Select(y=>y.Element("brandname").Value) // Transform it by getting the "brandname" element and reading it's Value.
.ToArray<string>()); // Then convert that into a string[]
Is that helpful? When I'm coding myself, I like to split long LINQ statements up by putting them on separate lines like this, and then I can just read down the lines: "I get a list WHERE this is true, then SELECT this other thing based on it, and turn it into an ARRAY."
In LINQ2XML
XElement doc=XElement.Load(@"..\..\stock.xml");
//put this code in the form_load event
cbProduct.Items.AddRange(doc.Descendants("items").Select(x=>x.Element("productname").Value).ToArray<string>());//adds all products
//put this code in the SelectedIndexChanged event of cbProduct
string product2Search=cbProduct.SelectedItem.ToString();//selected value of cbProduct
cbBrandNamedoc.Items.Clear(); //clears all items in cbBrandNamedoc
cbBrandNamedoc.Items.AddRange(doc.Descendants("items").Where(x=>x.Element("productname").Value==product2Search).Select(y=>y.Element("brandname").Value).ToArray<string>());