Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filling a property of list from another property of list using lambda?

Tags:

c#

lambda

linq

I have a list List<BidResult> bidresults filled with data. BidResult class is as below:

public class BidResult
{        
    public virtual int BidResultId { get; set; }
    public virtual string ProductName { get; set; }            
}

I have another list List<Rating> ratings which is empty and its properties are as follow:

public class Rating
{
    public virtual int RatingId { get; set; }            
    public virtual int BidResultId { get; set; }    

}

I need to populate 2nd list's BidResultId property using 1st list's BidResultId . I know it can be done using a loop. But I need to do it using Lambda preferably.

Edit:

I tried below:

 List<Rating> ratings = bidResults.Select(bidResult => new Rating {RatingId = bidResult.BidResultId}).ToList();

but I am not able to access BidResultId from bidResult.

Please note bidResult is a strongly typed list. Adding a . after bidResult show lambda functions like select, foreach, single etc.

like image 665
Toubi Avatar asked Jan 24 '14 06:01

Toubi


2 Answers

Try this-

   List<Rating> ratings = bidResults.Select(bidResult => new Rating {RatingId = bidResult.BidResultId}).ToList();

UPDATE 1:

   List<BidResult> bidResults = new List<BidResult>();
   bidResults.Add(new BidResult{BidResultId = 1,ProductName = "Product 1"});
   bidResults.Add(new BidResult { BidResultId = 2, ProductName = "Product 2" });
   bidResults.Add(new BidResult { BidResultId = 3, ProductName = "Product 3" });
   List<Rating> ratings = bidResults.Select(tempResult => new Rating { BidResultId = tempResult.BidResultId }).ToList();
like image 177
Ramashankar Avatar answered Oct 23 '22 11:10

Ramashankar


If bidResult => bidResult. is giving you LINQ methods, then it's likely that the bidResults that you are running the .Select on is some type of IEnumerable<<IEnumerable<BidResult>> (i.e., a collection of a collection of BidResult objects.

For that, you can use a Enumerable.SelectMany on the outer list to flatten it out and expose the inner collections to grab the BidResults:

ratings = bidResults
    .SelectMany(innerResults => innerResults)
    .Select(bidResult => new Rating { BidResultId = bidResult.BidResultId })
    .ToList();

To test this, I made a little console app:

List<Rating> ratings = null;
List<BidResult> tempList = new List<BidResult> {
    new BidResult { BidResultId = 1, ProductName = "First Product" },
    new BidResult { BidResultId = 2, ProductName = "Second Product" }
};

// creating a list of lists to simulate your issue
List<List<BidResult>> bidResults = new List<List<BidResult>> { tempList };

var i = 1; // just a variable to assign a rating id for the console output

ratings = bidResults
    .SelectMany(innerResults => innerResults) // required to flatten out the list of lists
    .Select(bidResult => new Rating { // bidResult is now a BidResult object
        RatingId = i++, 
        BidResultId = bidResult.BidResultId 
    }).ToList();

foreach (var result in ratings)
{
    Console.WriteLine("RatingID: {0} - BidResultID: {1}", result.RatingId, result.BidResultId);
}

Console.Read(); // don't exit until user hits a key

Which outputs the following:

RatingID: 1 - BidResultID: 1
RatingID: 2 - BidResultID: 2

For a graphical look at SelectMany and how it works, check out this blog post:

  • A Visual Look At The LINQ SelectMany Operator
like image 28
valverij Avatar answered Oct 23 '22 09:10

valverij