How to get some columns of entity in entity-framework?

c# entity-framework-6 linq sql

Question

Assume I have a table with more than 1000000 columns. When I use LINQ To SQL and Entity-Framework all queries will write in c# like below:

EFContext.MyTableName.Where(row=>row.column1==someValue)
                     .Select(...)
                     .FirstOrDefault(...)
                     .Any(...)
                     ...

How to get only and only some columns of entity? Are there any way to get only columns 1 & 2 & 3 among 1000000 columns for example?

Attention:

Type of resulted data should keep after selection, for example if without filtering some columns type of result is Type1 it's very important that after filtering, type of result be Type1 but value of those properties of Type1 which are filtered should be null or default.

1
3
3/11/2014 11:14:38 AM

Popular Answer

To only get a few ROWS you can use:

.Take(3);

To get only some columns you could use:

.Select(x => new MyType() { Column1 = x.Column1, Column2 = x.Column2 })

Note that the object now isn't attached to the objectcontext so it won't be affected by SaveChanges(). But you'll only have selected a few columns and the type will still be correct.

To get your own defaults instead of the framework defaults for variable types, you could modify your constructors type to set the defaults.

4
3/11/2014 11:22:52 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow