views:

28

answers:

1

Hi folks,

I've got a pretty simple Linq to Sql statement that (on he surface) is working fine. When I check the sql code that it generates, it's trying to retrieve all of the table fields instead of the fields that I just request. Is this normal practice?

here's some psedo code for the linq to sql query :-

var result = (from q in db.Foos
              where blah blah blah
              orderby more blah
              select new ResultThingy
              {
                  A = q.A, // int
                  B = q.B, // string
                  C = q.Bar.A // int
                  D = q.Bar.B // string
              })
              .Take(5)
              .ToList();

Now, it's retreiving all the values from table Bar ... (and a few other fields).

Is this normal practice? Notice how each property in the ResultThingy class is a simple type?

Hmmm... thoughts? I'm really confused over this.

A: 

This is normal (after looking at your code carefully).

As you are using a .NET type (not an entity), Linq2SQL has to extract everything up to the last select.

The get around this, use an anonymous object instead.

IOW: select new { q.A, q.B, C = q.Bar.A, D = q.Bar.B }

leppie
really? hmm. awesome i'll give this a go. I can confirm that my ResultThingy is just a _POCO_ class and not a Linq2SQL entity table/class thingy. I'll see what happens when i use an anonymous object instead and report back here.
Pure.Krome
It will work :) Your select is treated as an Enumerable and not a Queryable (but it is Queryable up to the select).
leppie
Nope - still tries to extract everthing :( the code changed to ... select new { ... } just like you did, above. So i stripped all the items in the select back, until i found which one(s) were causing the problem. Out of interested, i also tried stripping it back and instead of using an anonymous object (as suggested), i used my POCO. that worked, actually (until i included the 'bad' property that was causing all the data for that entity, to be returned). Hmm weird :) i'll give u the points anyways :)
Pure.Krome