如果我有一个示例课,请说X:
public class X
{
public string Id { get; set; }
public string Name { get; set; }
}
我从一个函数中获得了Id
和Name
的多个值:
var Xgroups = await client.Groups.GetGroupsAsync();
可以从Id
中选择Name
和Xgroups
,如下所示:
foreach (var group in Xgroups.Value)
{
ids.Add(group.Id); //ids is list of id
names.Add(group.Name); //names is list name
}
如何将这些值绑定到模型?是否应该像上面的代码行那样使用列表?我该如何实现?
var xgroups = new X() { Ids = ids, Names = names };
答案 0 :(得分:0)
如@Shyju所建议,您可以动态创建一个包含X个类的列表:
var list = new List<X>();
foreach (var group in Xgroups.Value)
{
list.Add(new X
{
Id = group.Id,
Name = group.Name
}
);
}
答案 1 :(得分:0)
This is converting your response from wcf service to List of class type
X.
var results = Xgroups.Value.Select(a=>new X{ Id = a.Id,Name = a.Name
}).ToList();
I would give a proper name to the model rather than X like Person,
GroupPerson.