Search code examples
c#linqnamevaluecollection

How can I use a LINQ projection with List<MyStructure> to get a NameValueCollection?


I have a

var list = new List<MyStructure>();

I want to extract MyStructure.Foo and MyStructure.Bar and put these two in a NameValueCollection. I want NameValueCollection because I am using WebClient.UploadValues()

The following creates an IEnumerable<NamedValueCollection>. I need just a NameValueCollection

var nameValueCollection = structure.Select(x => new NameValueCollection             
        { 
            { x.TypeId.ToString(), x.Value } 
        });

Thanks.


Solution

  • No need to use LINQ here

    foreach(var myStruct in list)
    {
       myNameValueCollection.Add(myStruct.Foo, myStruct.Bar);
    }