Get first item in list from linq query

前端 未结 3 1556
清酒与你
清酒与你 2020-12-10 16:58

I wish to get just the very first record I realize the the \"Take() will not work.

So I have a List that queries another list

List etch         


        
3条回答
  •  忘掉有多难
    2020-12-10 17:18

    Use the FistOrDefault method to safely return the first item from your query, or null if the query returned no results:

    var result =
        (from vio in AddPlas
         where etchList.Any(vioID => vio.Key.Formatted.Equals(vioID))
         select new
         {
            EtchVectors = vio.Shapes.FistOrDefault()
         })
    

    Or equivalently:

    var result = AddPlas.Where(x => etchList.Any(y => x.Key.Formatted.Equals(y)))
                        .Select(x => new { EtchVectors = x.Shapes.FistOrDefault() });
    

提交回复
热议问题