Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL - How to select specific columns and return strongly typed list

I'm trying to use LINQ to SQL to select a few specific columns from a table and return the result as a strongly typed list of objects.

For Example:

var result = (from a in DataContext.Persons                               where a.Age > 18                               select new Person                               {                                   Name = a.Name,                                   Age = a.Age                               }                               ).ToList(); 

Any help would be greatly appreciated.

It builds fine, but when I run it, I get the error. Explicit construction of entity type MyEntity in query is not allowed.

like image 854
Mike M Avatar asked Jul 07 '09 21:07

Mike M


People also ask

How do I get specific columns in Entity Framework?

We can do that simply by using the “new” operator and selecting the properties from the object that we need. In this case, we only want to retrieve the Id and Title columns.

What does Select in LINQ return?

The Select() method invokes the provided selector delegate on each element of the source IEnumerable<T> sequence, and returns a new result IEnumerable<U> sequence containing the output of each invocation.

What does the Select keyword specify in a LINQ query?

The select clause produces the results of the query and specifies the "shape" or type of each returned element.

What is the difference between where and Select in LINQ?

Select will always return the same number of elements in the list (regardless of a filter condition you may have). Where can return less elements depending on your filter condition.


2 Answers

Basically you are doing it the right way. However, you should use an instance of the DataContext for querying (it's not obvious that DataContext is an instance or the type name from your query):

var result = (from a in new DataContext().Persons               where a.Age > 18               select new Person { Name = a.Name, Age = a.Age }).ToList(); 

Apparently, the Person class is your LINQ to SQL generated entity class. You should create your own class if you only want some of the columns:

class PersonInformation {    public string Name {get;set;}    public int Age {get;set;} }  var result = (from a in new DataContext().Persons               where a.Age > 18               select new PersonInformation { Name = a.Name, Age = a.Age }).ToList(); 

You can freely swap var with List<PersonInformation> here without affecting anything (as this is what the compiler does).

Otherwise, if you are working locally with the query, I suggest considering an anonymous type:

var result = (from a in new DataContext().Persons               where a.Age > 18               select new { a.Name, a.Age }).ToList(); 

Note that in all of these cases, the result is statically typed (it's type is known at compile time). The latter type is a List of a compiler generated anonymous class similar to the PersonInformation class I wrote above. As of C# 3.0, there's no dynamic typing in the language.

UPDATE:

If you really want to return a List<Person> (which might or might not be the best thing to do), you can do this:

var result = from a in new DataContext().Persons              where a.Age > 18              select new { a.Name, a.Age };  List<Person> list = result.AsEnumerable()                           .Select(o => new Person {                                            Name = o.Name,                                             Age = o.Age                           }).ToList(); 

You can merge the above statements too, but I separated them for clarity.

like image 59
mmx Avatar answered Sep 20 '22 09:09

mmx


The issue was in fact that one of the properties was a relation to another table. I changed my LINQ query so that it could get the same data from a different method without needing to load the entire table.

Thank you all for your help!

like image 31
Mike M Avatar answered Sep 17 '22 09:09

Mike M