LINQ select one field from list of DTO objects to array

后端 未结 5 626
我寻月下人不归
我寻月下人不归 2020-12-05 02:31

I have DTO class that defines order line like this:

public class Line
{
    public string Sku { get; set; }
    public int Qty    { get; set; }
}
         


        
5条回答
  •  [愿得一人]
    2020-12-05 02:38

    You can use:

    var mySKUs = myLines.Select(l => l.Sku).ToList();
    

    The Select method, in this case, performs a mapping from IEnumerable to IEnumerable (the SKU), then ToList() converts it to a List.

    Note that this requires using System.Linq; to be at the top of your .cs file.

提交回复
热议问题