Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This piece of LINQ-to-XML that I found (LINQ To XML Tutorials with Examples | DotNetCurry[^]) appears to do exactly what I want. For consistency with the rest of the code I am working on, I'd like to express it using the LINQ lamda functions, not the keywords (i.e. .From() instead of from).

I confess to having real trouble when it comes to this type of problem, even though I know it should be trivial. Can anyone please tell me how I translate the following to use LINQ lamda functions?

var xmlfromLINQ = new XElement("customers",
            from c in customerlist
            select new XElement("customer",
                new XElement("CustomerId", c.CustomerId),
                new XElement("CustomerName", c.CustomerName),
                new XElement("PhoneNumber", c.PhoneNumber),
                new XElement("Email", c.Email)
                ));


What I have tried:

I've tried pretty much everything I can think of. I'm hoping a tea break gives me new insight.
Posted
Updated 12-Jul-19 1:01am
v2

1 solution

I have no idea what you mean by "standard C# LINQ functions"...

If you would like to use Non-linq solution, you can loop through the collection of customers by using for or foreach statement:

C#
List<XElement> nodes = new List<XElement>();
foreach(Customer c in customers)
{
    var node = new XElement(...);
    nodes.Add(node);
}


If you want to use Linq - lambda expression, try this:
C#
List<XElement> nodes = customers
    .Select(x=> new XElement(...))
    .ToList();
 
Share this answer
 
Comments
Patrick Skelton 12-Jul-19 7:02am    
Sorry, I did of course mean LINQ lamda functions. Thank you for the correction and the answer.
Maciej Los 12-Jul-19 7:04am    
You're very welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900