Linq : select value in a datatable column

前端 未结 7 492
广开言路
广开言路 2020-12-06 09:38

How do you use LINQ (C#) to select the value in a particular column for a particular row in a datatable. The equivalent SQL would be:<

7条回答
  •  不知归路
    2020-12-06 10:15

    I notice others have given the non-lambda syntax so just to have this complete I'll put in the lambda syntax equivalent:

    Non-lambda (as per James's post):

    var name = from i in DataContext.MyTable
               where i.ID == 0
               select i.Name
    

    Equivalent lambda syntax:

    var name = DataContext.MyTable.Where(i => i.ID == 0)
                                  .Select(i => new { Name = i.Name });
    

    There's not really much practical difference, just personal opinion on which you prefer.

提交回复
热议问题