score:3

Accepted answer

there are two ways of writing linq-queries, and though it doesn't really matter witch one you use it's good to know both of them cause they might learn you something about how linq works.

for instance, you have a set of jobs. if you were to select all jobs with an industryid of 5 (wild guess of data-types) you'd probably write something like this:

from j in dbconnection.jobs
where j.inustryid == 5
select j;

the very same query can also be written like this

dbconnections.jobs.where(j => j.industryid == 5);

now, i'm not here to preach saying one way is better than the other, but here you can clearly see how linq using the extension-methods syntax automatically selects on the iterated object (unless you do a select), whereas in the query-syntax you must do this explicitly. also, if you were to add inn another where clause here it would look something like this:

from j in dbconnection.jobs
where j.inustryid == 5 // not using && here just to prove a point
where j.cityid == 3 // i think this is valid syntax, i don't really use the query-syntax in linq
select j;

while in the extension-methods you can just append more method-calls like so:

dbconnections.jobs.where(j => j.industryid == 5)
     .where(j => j.cityid == 3);

now this is good to know cause this means you can just put your linq-query inside a function an continue querying it. and all you need to do to make it work in your case is just explicitly select the starting variable j, or all the variables you need like so:

var q =
    from j in dbconnection.jobs
     join i in dbconnection.industries on j.industryid equals i.id
     join c in dbconnection.cities on j.cityid equals c.id
     join s in dbconnection.states on j.stateid equals s.id
     join pt in dbconnection.positiontypes on j.positiontypeid equals pt.id;
             select new {j = j, i = i, c = c, s = s, pt = pt };
return q;

then you should be able to do for instance this:

 getjobsquery().where(a => a.i.id == 5); // i used a as a name for "all", like the collection of variables

or using the query-syntax

 from a in getjobsquery()
 where a.i.id == 5
 select a;

score:0

would this be better solved by returning a set of data (e.g. the common data) and querying for a subset of that data?

e.g. [pseudocode]

var alljobs =
    (from j in dbconnection.jobs
     join i in dbconnection.industries on j.industryid equals i.id
     join c in dbconnection.cities on j.cityid equals c.id
     join s in dbconnection.states on j.stateid equals s.id
     join pt in dbconnection.positiontypes on j.positiontypeid equals pt.id
     select j);

var myjobs = alljobs.orderby(j => j.issuedate).skip(expr).take(alljobs.count);

or similar...


Related Query

More Query from same tag