Sql – Which fields will be queried when using LINQ to SQL bind to a gridview

linq-to-sql

For example, I have a table TableA, with columns Field1, Field2 and Field3. My Linq-to-SQL code is:

from c in a.TableAs select c

Then I bind this query to a GridView named gvSample which only uses Field1 and Field2 of TableA.

Then, when the Linq-to-SQL query is enumerated, does the data of Field3 will be returned?

Best Answer

Simply, yes - you are selecting the full entity, so all mapped fields will be returned, regardless of how they are bound to up-stream components such as the GridView.

If you only wanted to return a subset of fields from the database then your query would be more like this:

from c in a.TableAs select new { Field1, Field2 }

This will select Field1 and Field2 only from the database table, into an anonymous type which is still suitable for binding to a GridView. You could also project into a well-known object if you didn't want to use an anonymous type:

from c in a.TableAs select new MyEntity() { Prop1 = c.Field1, Prop2 = c.Field2 }