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 Query
- How do I build up a LINQ => SQL / entities query (with joins) step-by-step?
- How do I most elegantly express left join with aggregate SQL as LINQ query
- How can I build Linq query with dynamic OR statements?
- 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
- How to build a nested query with the dynamic LINQ library
- How to get SQL query into LINQ form in C# code
- Convert sql query with multiple inner joins to LINQ
- How can I issue a LINQ query to a SQL Server to check if a column value starts with a word?
- How to convert this complex SQL Query with Subqueries into LINQ
- How to write SQL SELECT INNER JOIN with multiple conditions (with LIKE) query to LINQ to SQL
- How to convert SQL query to Linq with GROUP BY on only date part of a datetime?
- Linq to Sql - Query list of distinct items with multiple joins
- LINQ syntax for SQL query with multiple inner joins and aliases
- How to return a child collection with joins linq query
- How to rewrite this LINQ query with SQL syntax and <>?
- How to convert a SQL with multiple left joins to an Entity Framework LINQ statement using Include methods (No Join methods)?
- how to write a Linq query with a EF code first Many to Many relationship
- SQL to Linq query with multiple left outer joins
- How to transform grouping with MAX from sql to linq query
- How can I convert Sql query to Linq and its equivalent with Join() method in Entity Framework Core
- How To Write Distance LINQ query i have Lat, Long in table with varchar type SQL Server
- How to simplify this LINQ to Entities Query to make a less horrible SQL statement from it? (contains Distinct,GroupBy and Count)
- How to set Filter Values in LinQ with SQL Query Text?
- How to LINQ Query Code First generated EF6 hierarchical entities (entities within entities)?
- How do I convert SQL Query with Ccase Switch to Linq Lambda
- How to convert SQL query to LINQ with Orderby, Groupby
- How to build a LINQ to XML query with a conditional selection
- How to convert multiple SQL joins to LINQ query
- using linq query with include/then_include vs using sql query with joins and gettng db server error
More Query from same tag
- Remove set of elements from list A and add into list B using Linq
- how can i pass parameter for Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null,
- LINQ for grabbing Tuple values and aggregating, etc. into new tuple?
- Seemingly simple LINQ query is throwing me off. I want all items in array A unless it exists in array B
- I want take the last 4 record but always get the first 4
- any log about db queries executed in my site?
- How to do a Sum using Dynamic LINQ
- Join Datatables via linq
- Get relations with conditions in Entity Framework Core
- Linq - Join 2 tables and Select all columns VB.Net
- Using ternary operator to check if null - null reference is not the same as null
- Separating multiple XML results in an XDocument
- LINQ to Entities does not recognize the method 'System.Linq.IQueryable in nested select
- C# Linq error 'DbSet() .GroupJoin( inner: DbSet(), outerKeySelector: ' could not be translated
- Querying XML with LINQ and using null in place of a particular xml attribute if it does not exist
- Find distinct column id and column count with condition
- This code seems to return all rows
- Joining two tables with LINQ while also returning null records from the second table
- Select and show only parent node based on child selection
- Converting for-loop to a linq query
- How can I write this with where clause where CreatedDate is in between Date1var & Date2var?
- Null check for where clause in EF
- How to map data to class structure using linq?
- grouping data from two dataset
- How to conditionally read elements in LINQToXML
- Add minutes from one column to datetime column in linq query
- Updating object Linq'd from collection doesn't update the object in collection
- Get Count of Lazy Loaded Child Collection Using LINQ and Entity Framework
- LINQ to Entities: Summing child value
- For vs. Linq - Performance vs. Future