Return list of specific property of object using linq

后端 未结 3 749
说谎
说谎 2020-12-25 10:15

Given a class like this:

public class Stock
{
    public Stock() {}
    public Guid StockID { get; set; }
    public string Description { get; set; }
}


        
相关标签:
3条回答
  • 2020-12-25 10:31

    How about something like this? Can this be simplified?

    List<TranCode> alltrans = new List<TranCode>();
    
    foreach (var bh in obj1.obj2WithCollection.obj2Collection)
    {
        alltrans.AddRange(bh.obj3WithCollection.Select(e => e.TransactionCode).ToList());
    }
    
    0 讨论(0)
  • 2020-12-25 10:33

    You could also do it like this:

    ids = stockItems.ConvertAll<Guid>(o => o.StockID);
    
    0 讨论(0)
  • 2020-12-25 10:34
    var list = stockItems.Select(item => item.StockID).ToList();
    
    0 讨论(0)
提交回复
热议问题