Reputation: 333
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?
Upvotes: 1
Views: 13368
Reputation: 856
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();
Upvotes: 0
Reputation: 388073
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
};
Upvotes: 1
Reputation: 30042
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
.
Upvotes: 3
Reputation: 2076
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();
Upvotes: 0