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

StackOverflow https://stackoverflow.com/questions/17233033

  •  01-06-2022
  •  | 
  •  

سؤال

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.

هل كانت مفيدة؟

المحلول

No need to use LINQ here

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

نصائح أخرى

Try the following.

        class MyStructure
    {
        public string Foo { get; set; }
        public string Bar { get; set; }
    }

    public static void Main(string[] args)
    {

        var list = new List<MyStructure>();
        NameValueCollection nameValueCollection = new NameValueCollection();

        list.ForEach(x => {
            nameValueCollection.Add(x.Foo, x.Bar);
        });

    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top