score:3
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...
Source: stackoverflow.com
Related Articles
- Casting to a derived type in a LINQ to Entities query with Table Per Hierarchy inheritance
- Can't get c# linq query to compile with joins
- Dynamically build select list from linq to entities query
- How can I build Linq query with dynamic OR statements?
- Problem with LINQ to Entities query using Sum on child object property
- Linq to entities - SQL Query - Where list contains object with 2 properties (or more)
- How to write a LINQ to Entities query with List in a "WHERE" condition
- LINQ query with two joins that worked in EF 6 gives error in EF 7
- C# Linq Query with Multiple Joins with an await
- How to build a nested query with the dynamic LINQ library
- LINQ join query with relational entities
- Linq Query With Multiple Joins Not Giving Correct Results
- Dynamically build up a linq query with hierarchy using string field names?
- Convert sql query with multiple inner joins to LINQ
- Linq query with two joins on inner select in EF
- Linq sub query when using a repository pattern with EF code first
- Dynamics CRM 2011 - Filtering LINQ query with outer joins
- LINQ query to get all entities with member of specified type
- Multiple JOINS with Conditions between them : LINQ query
- LINQ query on entities with join returning items by category
- How do I build up a LINQ => SQL / entities query (with joins) step-by-step?
- LINQ Entities build Query at Runtime 'The parameter is not in scope.' LinqKit
- Linq to Sql - Query list of distinct items with multiple joins
- Is there any way to encapsulate a simple LINQ query in an extension method that can be used with LINQ to Entities query?
- Linq to entities query with nested query on the same table
- Avoiding repeating code with Linq query + optional params
- Optimizing a LINQ query with multiple joins and predicates
- Linq build query dynamically with no concrete Type
- optimize linq query with related entities
- LINQ to Entities query with join inside method for use in MVC app
- How to customize a specific code for pagination when working with datagridview virtual mode on
- Dynamic Linq Core OrderBy Nullable Child Property
- C# linq operator as variable
- In C# can you perform a Range Select within a Range Select?
- How to select distinct with linq when comparing values
- How to get nested key value pair from dictionary with linq
- how to set IDENTITY_INSERT as ON?
- How do I use Linq to group a DataTable by multiple rows/columns into a new datatable with duplicate rows concatenated?
- Linq query with Average
- Performant way to fetch all rows inside and outside parent record
- Creating database using DataContext and System.Data.SQLite in C#
- LINQ fastest way to update lots of records (>2m)
- Building a Sensor Monitoring System using RX
- How to fix my LINQ to properly find strings from the list in string?
- Linq statement in C# to extract data from XElement
- Linq query with list of flags
- C# XML Linq, reading XML returns NullReferenceException
- Easiest way to convert IGrouping to IHierarchicalDataSource
- Return a parent entity together with its child entities using LINQ method syntax
- Isn't Cast<T> implementation against Linq's immutable style?