1

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?

6
  • 2
    Do you mean you're trying to transform a List<ProviderDetail> into a List<ProviderQualificationDetail> just by creating new instance of ProviderQualificationDetail> an copying the properties? You should look at LINQ, and in particular the Select and ToList methods. Commented Nov 16, 2016 at 7:50
  • How to get TotalServiceableOffers from ServiceableOffers ? Concatenate with some delimiter ? Group by ProviderName ? Or Is it the same value with different property name ? Commented Nov 16, 2016 at 7:51
  • @JonSkeet yes. I want to assign List<ProviderDetail> to List<ProviderQualificationDetail>. Is it possible? Commented Nov 16, 2016 at 7:52
  • @user3185569 same value with different property name Commented Nov 16, 2016 at 7:52
  • Just assigning it? No, absolutely not. Creating a new list, and populating it by converting each element of the old list? Yes. Commented Nov 16, 2016 at 7:53

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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
};

Comments

0

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();

Comments

0

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.