Ada
Ada

Reputation: 624

How do I get a list of an Object?

I am very new at Linq. I have a problem. I am trying to retrieve a list of Files. Here is the query I wrote.

var DataSource = from d in db.Directories
                 join dok in db.Files on d.DirectoryId equals dok.DirectoryId
                 where dok.SomeId == (int)cboSome.SelectedValue
                 select new  { d };

This retrieves the right d's but I want to retrieve a List of Files. When I take the datasource by casting var to Files, it returns null. How can I do that? Sorry if it is too simple.

Upvotes: 0

Views: 60

Answers (2)

Felipe Oriani
Felipe Oriani

Reputation: 38598

What result do you want?

If you want to result the objects from db.Directories, try something like this:

 var DataSource = (from d in db.Directories
                             join dok in db.Files on d.DirectoryId equals dok.DirectoryId
                             where dok.SomeId == (int)cboSome.SelectedValue
                             select d).ToList();

Remember to call the ToList() method.

Upvotes: 3

Chris
Chris

Reputation: 27599

There is a .ToList() extension method that should convert any IEnumerable into a List.

eg var List = DataSource.ToList();

Upvotes: 2

Related Questions