1

I have a data table and I need to convert the contents of that data table to a class List and I used Linq for that.It worked well.But I was asked to convert that linq query to Lambda Expression and there I had a Little trouble while using Let.I will the sample code.

Working linq query:

var NewUser = (from dt in dsMappedDataFields.Tables[0].Rows.Cast<DataRow>()
               let tempDetails = dt.Field<string>("Name") == "Rojer" ? "NW" : "India"
               let tempNumber = tempDetails == "India" ? "918956" : "0456"
               select new User
                        {
                           Name = dt.Field<string>("Name"),
                           Age = dt.Field<int>("Age"),
                           Details = tempDetails,
                           Number = tempNumber
                        }).ToList();

Lambda expression:

var User = dsMappedDataFields.Tables[0].Rows.Cast<DataRow>().

                             Select(dr =>
                                 new User
                                 {
                                     Name = dr.Field<string>("Name"),
                                     Age = dr.Field<int>("Age"),
                                     Details = dr.Field<string>("Details"),
                                     Number = dr.Field<string>("Number")
                                 }).ToList();

As you can see I have to check some conditions before converting the data to list which I have done earlier.. Please do help me with solving this issue.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Intruder
  • 147
  • 1
  • 12
  • http://stackoverflow.com/questions/1092687/code-equivalent-to-the-let-keyword-in-chained-linq-extension-method-calls – Karthik Aug 14 '15 at 07:25

2 Answers2

4

The lambda you pass to Select can have a block, so you can add any kind of code there:

var User = dsMappedDataFields.Tables[0].Rows
    .Cast<DataRow>()
    .Select(dr =>
    {
        var tempDetails = dt.Field<string>("Name") == "Rojer" ? "NW" : "India";
        var tempNumber = tempDetails == "India" ? "918956" : "0456";
        return new User
        {
            Name = dr.Field<string>("Name"),
            Age = dr.Field<int>("Age"),
            Details = tempDetails,
            Number = tempNumber,
        };
    })
    .ToList();
poke
  • 369,085
  • 72
  • 557
  • 602
  • a semicolon (;) needs to be added right after the close bracket (}) of the return statement. Also, the collection object before the .Select statement must be converted to ToList() or AsEnumerable(). That is because IQuerable or expression tree does not work with multi line statement body. – Thomas.Benz Jan 02 '20 at 18:01
  • @Thomas.Benz You’re right about the semicolon, I missed that. As for queryables, my assumption is that this is a `DataTable` which [`Rows`](https://learn.microsoft.com/en-us/dotnet/api/system.data.datatable.rows?view=netframework-4.8) property is a collection, and not something you can run queries on. – poke Jan 03 '20 at 13:55
-1

We can use lambda expression in this case :

var User = dsMappedDataFields.Tables[0].Rows.Cast <DataRow>().
Select(dr = > new User {
    Name = dr.Field<string>("Name"),
    Age = dr.Field<int>("Age"),
    Details = dr.Field<string>("Name") == "Rojer" ? "NW" : "India",
    Number = dr.Field<string>("Name") == "Rojer" ? 0456 : 918956,
}).ToList();    

I tried the sample it works as you need.

Thanks, Narasimha