Does .Net Core 2.1 Support LINQ and Lambda Expressions?

Viewed 3928

I have tried the following code on Visual Studio Code with OmniSharp extension has been installed. I installed .Net core 2.1.3.

List<MyType> listName = dataTableName.AsEnumerable().Select(m => new MyType()
{
    ID = m.Field<string>("ID"),
    Description = m.Field<string>("Description"),
    Balance = m.Field<double>("Balance"),
}).ToList()

But it seem not to be able use AsEnumerable(). And the message "Cannot convert lambda expression to type 'string' because it is not a delegate type" was showed. These are my using:

using System.Linq;
using System.Data;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using RestfulAPI.Commons;
using RestfulAPI.Models.DBModels;

How can I use LinQ on .Net Core? LinQ seem to be supported by .Net Framework. But I want my webservices API can run on Linux server. Have risk if use your solution, I cannot run my webservices on Linux server?

1 Answers

DataTable does not implement an Enumerator in .NET Core 2.1. This issue is open in Dotnet Github, so might be implemented some day.

This means you will need to use a loop to iterate over the rows. You cannot use foreach because DataTable has not implemented IEnumerable either.

For example:

for(int i = 0; i < dataTable.Rows.Count; i++)
{
    var row = dataTable.Rows[i];

    var temp = new MyType(){
        ID = row["ID"],
        Description = row["Description"],
        Balance = Double.Parse(row["Balance"].ToString())
     };

     retList.Add(temp);     
}

You can also write your own AsEnumerable extension method:

public static IEnumerable<DataRow> AsEnumerable(this DataTable table)
{
    for (int i = 0; i < table.Rows.Count; i++)
    {
        yield return table.Rows[i];
    }
}
Related