No need to use LINQ here
foreach(var myStruct in list)
{
myNameValueCollection.Add(myStruct.Foo, myStruct.Bar);
}
سؤال
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);
});
}