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 providerQualification = reporting.GetProviderQualification();
Now I want to copy this list to my List<ProviderQualificationDetail>
. How would I do that?
Using 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
.
You need a mapping to get from ProviderDetail
to 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 >)
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
. If she has ProviderQualification
class why she wants to bind to ProviderQualificationDetail
?
List<ProviderQualificationDetail> list=providerQualification.ProviderDetails.GroupBy(x => x.ProviderName)
.Select(x=>new ProviderQualificationDetail{
ProviderName =x.Key,
TotalServiceableOffers=Sum(y=>y.ServiceableOffers)
}).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.