[英]Assign a List object to another List in c#
I have following classes: 我有以下课程:
public class ProviderQualification
{
public List<ProviderDetail> ProviderDetails { get; set; }
}
public class ProviderDetail
{
public string ProviderName { get; set; }
public string ServiceableOffers { get; set; }
}
public class ProviderQualificationTimeViewModel
{
public List<ProviderQualificationDetail> ProviderQualificationDetails { get; set; }
}
public class ProviderQualificationDetail
{
public string ProviderName { get; set; }
public string TotalServiceableOffers { get; set; }
}
I have ProviderQualification
object populated with List<ProviderDetail>
. 我有
ProviderQualification
对象填充List<ProviderDetail>
。
ProviderQualification providerQualification = reporting.GetProviderQualification();
Now I want to copy this list to my List<ProviderQualificationDetail>
. 现在,我想将此列表复制到我的
List<ProviderQualificationDetail>
。 How would I do that? 我该怎么办?
Using Linq: 使用Linq:
List<ProviderQualificationDetail> result =
providerQualification.ProviderDetails.Select(
q => new ProviderQualificationDetail()
{
ProviderName = q.ProviderName,
TotalServiceableOffers = q.ServiceableOffers
}).ToList();
Using Select
you project each element in ProviderDetails
into a new element of type ProviderQualificationDetail
and then Enumerate that into a List
. 使用
Select
你的项目中的每个元素ProviderDetails
成类型的新元素ProviderQualificationDetail
,然后枚举成一个List
。
You need a mapping to get from ProviderDetail
to ProviderQualificationDetail
. 您需要从
ProviderDetail
到ProviderQualificationDetail
的映射。 For example, if you just want to copy over those values, you can just write it inline like this: 例如,如果您只想复制这些值,则可以像这样内联地编写它:
ProviderQualification providerQualification = reporting.GetProviderQualification();
var items = providerQualification.ProviderDetails
.Select(detail => new ProviderQualificationDetail
{
ProviderName = detail.ProviderName,
TotalServiceableOffers = detail.ServiceableOffers
})
.ToList();
var viewModel = new ProviderQualificationTimeViewModel
{
ProviderQualificationDetails = items
};
You can use 您可以使用
Enumerable.Select< TSource, TResult > Method (IEnumerable< TSource >, Func< TSource, TResult >)
Enumerable.Select <TSource,TResult>方法(IEnumerable <TSource>,Func <TSource,TResult>)
For example 例如
List<ProviderQualificationDetail> list =
providerQualification.ProviderDetails
.Select(x => new ProviderQualificationDetail(){
ProviderName = x.ProviderName,
TotalServiceableOffers = x.ServiceableOffers
}).ToList();
In my opinion, @zaria want to select distinct ProviderName
and total ServiceableOffers
. 我认为@zaria要选择不同的
ProviderName
和总的ServiceableOffers
。 If she has ProviderQualification
class why she wants to bind to ProviderQualificationDetail
? 如果她具有
ProviderQualification
类,为什么要绑定到ProviderQualificationDetail
?
List<ProviderQualificationDetail> list=providerQualification.ProviderDetails.GroupBy(x => x.ProviderName)
.Select(x=>new ProviderQualificationDetail{
ProviderName =x.Key,
TotalServiceableOffers=Sum(y=>y.ServiceableOffers)
}).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.