Royi Namir
Royi Namir

Reputation: 148744

Simplify my C# Linq statement?

class :

class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

List

List<Foo> lst = new List<Foo>();

Datatable :

 DataTable dt = GetFromDb ()....

I want to fill lst with records from dt.

I've managed doing :

Array.ForEach(dt.AsEnumerable().ToArray(), y = > lst.Add(new Foo()
{
    Id = int.Parse(y["id"].ToString()), Name = y["name"].ToString()
}));

question :

  1. Can't I do something else like dt.AsEnumerable().Select(_ => fill lst ) ?

I know that part of the select signature (in this case ) is Func<datarow,void> which wont compile

But still , is there any other way of doing this besides the ugly way of mine ?

Upvotes: 2

Views: 256

Answers (5)

Royi Namir
Royi Namir

Reputation: 148744

I got another solution via Rows property :

 List<Foo> results = (from DataRow myRow in dt.Rows
                               select new Foo()
                            {
                              Id =int.Parse( row["id"].ToString()),
                              Name = row["name"].ToString()
                             }).ToList();

Upvotes: 1

Lucas
Lucas

Reputation: 17434

Using LINQ to DataSet:

var foos = from row in dt.AsEnumerable()
           select new Foo()
           {
              Id = row.Field<int>("id"),
              Name = row.Field<string>("name")
           };

// create a new list
List<Foo> lst = foos.ToList();

// update: add items to an exisiting list
fooList.AddRange(foos);

Upvotes: 7

Shagglez
Shagglez

Reputation: 1522

You are on the right track, you can do the following:

lst = dt.AsEnumerable().Select(y = > new Foo()
{
    Id = Convert.ToInt32(y["id"]), Name = y["name"] as string
}).ToList();

EDIT: To add to an existing list

I would normally just concatenate two lists:

lst = lst.Concat(dt.AsEnumerable().Select(...)).ToList();

However having seen ken2k's answer, I think I will now start using AddRange instead.

Upvotes: 4

Shyju
Shyju

Reputation: 218942

Try this

var fooList = (from t in dt.AsEnumerable() 
         select new Foo { Id = t.Field<int>("Id"), 
                          Name= t.Field<string>("Name") }).ToList();

The AsEnumerable extension method on a DataTable will return a Collection of DataRows. then you do a projection from your Linq Result with required fields. Since it is dataRow type, you need to specify what each column type will be.

Upvotes: 7

ken2k
ken2k

Reputation: 49013

You could do:

List<Foo> lst = dt.AsEnumerable().Select(z => new Foo
{
    Id = int.Parse(z["id"].ToString()),
    Name = z["name"].ToString()
}).ToList();

EDIT:

If you want to append to an existing List<Foo> instance:

lst.AddRange(dt.AsEnumerable().Select(z => new Foo
{
    Id = int.Parse(z["id"].ToString()),
    Name = z["name"].ToString()
}));

Upvotes: 2

Related Questions