使用lambda从列表的另一个属性填充列表的属性?

时间:2014-01-24 06:37:20

标签: c# linq lambda

我有一个填充数据的列表List<BidResult> bidresultsBidResult课程如下:

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

我有另一个列表List<Rating> ratings,它是空的,其属性如下:

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

}

我需要使用第一个列表的BidResultId填充第二个列表的BidResultId属性。 我知道可以使用循环来完成。但我需要最好使用Lambda。

修改

我试过以下:

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

但我无法从BidResultId访问bidResult

请注意bidResult是一个强类型列表。添加一个。在bidResult之后显示lambda函数,如select,foreach,single等。

6 个答案:

答案 0 :(得分:3)

试试这个 -

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

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

答案 1 :(得分:2)

如果bidResult => bidResult.为您提供LINQ方法,那么您运行bidResults的{​​{1}}可能是某种类型的.Select(即一个集合) IEnumerable<<IEnumerable<BidResult>>个对象的集合。

为此,您可以使用外部列表中的Enumerable.SelectMany将其展平并展示内部集合以抓取BidResult

BidResults

为了测试这个,我做了一个小控制台应用程序:

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

其中输出以下内容:

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

有关RatingID: 1 - BidResultID: 1 RatingID: 2 - BidResultID: 2 的图形查看及其工作原理,请查看此博文:

答案 2 :(得分:1)

Ramashankar的答案在语法上对我来说是正确的(虽然我不确定你真的想将BidResult.BidResultId指派给Rating.RatingId)。

所以剩下的问题是为什么这不适合你。我最好的猜测是一场范围界定的冲突。周围的代码块是否可能包含名为bidResult的变量?在这种情况下,此变量将与lambda输入运算符冲突。这可以解释为什么你得到那些lambda函数。使用bidResult。因此解决方案是更改lambda输入运算符的名称(就像在Ramashankar的更新中一样)。

答案 3 :(得分:0)

这是您答案的解决方案。

BidResult bidResult = new BidResult();
List<BidResult> bidResultList = new List<BidResult>();

// Lets Adding Some Data to bidResultList
bidResult.BidResultId = 1;
bidResult.ProductName = "A";
bidResultList.Add(bidResult);
bidResult.BidResultId = 2;
bidResult.ProductName = "B";
bidResultList.Add(bidResult);
bidResult.BidResultId = 3;
bidResult.ProductName = "C";
bidResultList.Add(bidResult);


List<Rating> ratingList = bidResultList.Select(bid => new Rating { BidResultId = bid.BidResultId }).ToList();

如果你想发起ratingId(例如7开始),那么使用上面的表达式。

List<Rating> ratingList = bidResultList.Select(bid => new Rating { RatingId=7, BidResultId = bid.BidResultId }).ToList();

答案 4 :(得分:0)

您也可以使用AutoMapper

List<BidResult> bidresults = new List<BidResult>() {
    new BidResult() { BidResultId = 1, ProductName = "hello"},
    new BidResult() { BidResultId = 2, ProductName = "world"}
};

///////
Mapper.CreateMap<BidResult, Rating>();

var ratings = Mapper.Map<List<Rating>>(bidresults);
///////

foreach (var rating in ratings)
    Console.WriteLine("{0},{1}", rating.BidResultId, rating.RatingId);

对外输出

1,0
2,0

答案 5 :(得分:0)

试试这个

List<Ratings> ratings = new List<Ratings>();
ratings.AddRange(bidresults.Select(bidResult => new Rating {RatingId = bidResult.BidResultId}).ToArray());