Search code examples
linq-to-dataset

Finding a column in a DataTable row


 Client_Payroll_Items.AsEnumerable().Select((row, index) => new {row, index})
    .Where(
            (x => x.row.Client_KEY == 3)   &&
            (x => x.row.Description == "401(k) % of Gross")
    )
    //.Where(x => x.row.Description == "401(k) % of Gross")
   .Select(x=> x.row.Payroll_item_calculation_type_KEY)
   .DefaultIfEmpty(-1)
   .First();

When I am running it I am getting "Operator '&&' cannot be applied to operands of type 'lambda expression' and 'lambda expression'" error.

 Client_Payroll_Items
    .AsEnumerable()
    .Select((row, index) => new {row, index})
    .Where(x => x.row.Client_KEY == 3)
    .Where(x => x.row.Description == "401(k) % of Gross")
    .Select(x=> x.row.Payroll_item_calculation_type_KEY)
    .DefaultIfEmpty(-1)
    .First()

The latest one runs and I get what I need.

Question - why the error in the first code?


Solution

  • In your first code sample you have specified the lambda x => multiple times, which is incorrect:

    .Where((x => x.row.Client_KEY == 3) && (x => x.row.Description == "401(k) % of Gross"))
    

    It should be specified only once and can be used multiple times, as needed, within the predicate:

    .Where(x => x.row.Client_KEY == 3 && x.row.Description == "401(k) % of Gross")