Lambda expression for multiple join tables with dynamic where clause - linq

Here is sample Linq statement:
var query = (from fm in db.table1
join fe in db.table2 on fm.Id equals fe.id
where (fe.field1 >= 200)
select new { fm, fe });
So i need to write where clause "field1" Dynamically and also conditions(> , <, >=, <=) with constant value "200"
I am new to Web Api. any one please help in this.

Related

How to convert my TSQL query to LINQ

I am new to linq. I can't convert this SQL code to linq. Please help me. Thanks.
DECLARE #myHID BIGINT;
SET #myHID = 1;
WITH tblChild AS
(
SELECT *
FROM wbs.WBS w
WHERE w.ParentId = #myHID
UNION ALL
SELECT w2.*
FROM wbs.WBS w2
JOIN tblChild ON w2.ParentId = tblChild.hID
)
SELECT
tblChild.hID ,
Unit.ID, w3.wbsName + ' * ' + tblChild.wbsName as
structure ,
tblChild.FK_WbsBaseStructure_hID ,
tblChild.parentID ,
unitNumber ,
unitTitle ,
FK_UsageItem_ID,
usageTitle ,
nominalArea
FROM
tblChild
INNER JOIN
unit.Unit ON tblChild.hID = Unit.FK_WBS_hID
INNER JOIN
unit.UsageItem ON Unit.FK_UsageItem_ID = UsageItem.ID
LEFT JOIN
wbs.WBS w3 ON tblChild.parentID = w3.hID
Please convert this to linq code.
Thanks.
For translating SQL to LINQ query comprehension:
Translate FROM subselects as separately declared variables.
Translate each clause in LINQ clause order, leaving monadic operators (DISTINCT, TOP, etc) as functions applied to the whole LINQ query.
Use table aliases as range variables. Use column aliases as anonymous type field names.
Use anonymous types (new { }) for multiple columns
Left Join is simulated by using a into join_variable and doing another from from the join variable followed by .DefaultIfEmpty().
Replace COALESCE with the conditional operator and a null test.
SELECT * must be replaced with select range_variable or for joins, an anonymous object containing all the range variables.
SELECT fields must be replaced with select new { ... } creating an anonymous object with all the desired fields or expressions.
Proper FULL OUTER JOIN must be handled with an extension method.

How can I get distinct results from this LINQ query?

Below is my ERD and sample data. Note, I'm using Entity Framework and Code first to control my database.
For the project named Vacation, return all the DISTINCT users who have a "true" value in UserBooleanAttributes table for either the Parents or Teens rows defined in the UserAttributes table.
Here is my current attempt:
var myQuery =
from P in context.Projects
join UA in context.UserAttributes on P.ProjectID equals UA.ProjectID
join UBA in context.UserBooleanAttributes on UA.UserAttributeID equals UBA.UserAttributeID
join U in context.Users on UBA.UserID equals U.UserID
where P.ProjectID == 1
where UBA.Value == true
where (UA.UserAttributeID == 1 || UA.UserAttributeID == 2)
select new { uba = U };
This returns 6 users, with e#acme.org being listed twice. Is there a LINQ way of returning distinct values? I suppose I could convert this to a list then filter, but I'd rather have the Database do the work.
I'd rather avoid using lambda expressions if possible. Once again, I want the database to do the work, and not have to write code to union/intersect result groups.

NHibernate Linq query join subquery on two column

A "case" can have many action types and each action is logged in a journal.
I want to show a list of "cases" only with the latest journal entry
This is the T-SQL sub query
SELECT SagId, max(Dato) as maxdate FROM vOpgaveliste o group by SagId
And this is the T-SQL main query
select o.* from
(
SELECT SagId, max(Dato) as maxdate
FROM vOpgaveliste o
group by SagId
)
as nyeste
join vOpgaveliste o on o.SagId = nyeste.SagId and o.Dato = nyeste.maxdate
I can create the subquery in linq
var queryInner = from o in query
where o.SagsbehandlerInit == "chr"
where o.Dato >= DateTime.Today && o.Dato <= DateTime.Today.AddDays(-7)
group o by o.SagId
into g
select new { SagId = g.Key, MaxDate = g.Max(d => d.Dato) };
I then created this query
var outer = from o in query
from s in queryInner
where s.SagId == o.SagId && s.MaxDate == o.Dato
select o;
But NHibernate throws a System.NotSupportedException was unhandled by user code exception
I also tried this syntax https://stackoverflow.com/a/16918106/1147577 but get an syntax error on the join statement
Thanks
NHibernate simply does not support sub queries within from statement block. It only supports it in the Select and Where block.
I guess you have to figure out another way to get your result.
Of course the linq to object or many other linq providers support all kind of crazy query constructs, the linq to Nhibernate implementation simply has tons of limitations.

Entity Framework LINQ - Subquery with Group By

I am trying to achieve a query which includes a subquery which itself includes grouping.
I based my code from answers to this question
The purpose of the code is to perform a simple de-duplication of the 'person' table based on the email address and return the latest person row.
var innerQuery = (from p in db.Person
join r in db.Registration on p equals r.Person
join e in db.EventDetail on r.EventDetail equals e
where e.Client.ClientID == clientID
group p by p.Email into g
select g.Max(p => p.PersonID));
var query = (from p2 in db.Person where innerQuery.Contains(p2.PersonID) select p2);
When the query is attempted to execute, I get the following error message:
LINQ to Entities does not recognize
the method 'Boolean
Contains[Int32](System.Linq.IQueryable`1[System.Int32],
Int32)' method, and this method cannot
be translated into a store expression.
I have tested the innerquery and it just returns a list of ints as expected, but the query fails with the above message.
Any help greatly appreciated.
Isn't query just a join?
var query = from p2 in db.Person
join iq in innerQuery on p2.PersonID equals iq
select p2;
I'm not sure about = iq part but I don't usually use that syntax sorry - in the other form it would be
.Join(innerQuery, p2 => p2.PersonId, iq => iq, (p2, iq) => p2);
for the join and the select.

Linq to entities Left Join

I want to achieve the following in Linq to Entities:
Get all Enquires that have no Application or the Application has a status != 4 (Completed)
select e.*
from Enquiry enq
left outer join Application app
on enq.enquiryid = app.enquiryid
where app.Status <> 4 or app.enquiryid is null
Has anyone done this before without using DefaultIfEmpty(), which is not supported by Linq to Entities?
I'm trying to add a filter to an IQueryable query like this:
IQueryable<Enquiry> query = Context.EnquirySet;
query = (from e in query
where e.Applications.DefaultIfEmpty()
.Where(app=>app.Status != 4).Count() >= 1
select e);
Thanks
Mark
In EF 4.0+, LEFT JOIN syntax is a little different and presents a crazy quirk:
var query = from c1 in db.Category
join c2 in db.Category on c1.CategoryID equals c2.ParentCategoryID
into ChildCategory
from cc in ChildCategory.DefaultIfEmpty()
select new CategoryObject
{
CategoryID = c1.CategoryID,
ChildName = cc.CategoryName
}
If you capture the execution of this query in SQL Server Profiler, you will see that it does indeed perform a LEFT OUTER JOIN. HOWEVER, if you have multiple LEFT JOIN ("Group Join") clauses in your Linq-to-Entity query, I have found that the self-join clause MAY actually execute as in INNER JOIN - EVEN IF THE ABOVE SYNTAX IS USED!
The resolution to that? As crazy and, according to MS, wrong as it sounds, I resolved this by changing the order of the join clauses. If the self-referencing LEFT JOIN clause was the 1st Linq Group Join, SQL Profiler reported an INNER JOIN. If the self-referencing LEFT JOIN clause was the LAST Linq Group Join, SQL Profiler reported an LEFT JOIN.
Do this:
IQueryable<Enquiry> query = Context.EnquirySet;
query = (from e in query
where (!e.Applications.Any())
|| e.Applications.Any(app => app.Status != 4)
select e);
I don't find LINQ's handling of the problem of what would be an "outer join" in SQL "goofy" at all. The key to understanding it is to think in terms of an object graph with nullable properties rather than a tabular result set.
Any() maps to EXISTS in SQL, so it's far more efficient than Count() in some cases.
Thanks guys for your help. I went for this option in the end but your solutions have helped broaden my knowledge.
IQueryable<Enquiry> query = Context.EnquirySet;
query = query.Except(from e in query
from a in e.Applications
where a.Status == 4
select e);
Because of Linq's goofy (read non-standard) way of handling outers, you have to use DefaultIfEmpty().
What you'll do is run your Linq-To-Entities query into two IEnumerables, then LEFT Join them using DefaultIfEmpty(). It may look something like:
IQueryable enq = Enquiry.Select();
IQueryable app = Application.Select();
var x = from e in enq
join a in app on e.enquiryid equals a.enquiryid
into ae
where e.Status != 4
from appEnq in ae.DefaultIfEmpty()
select e.*;
Just because you can't do it with Linq-To-Entities doesn't mean you can't do it with raw Linq.
(Note: before anyone downvotes me ... yes, I know there are more elegant ways to do this. I'm just trying to make it understandable. It's the concept that's important, right?)
Another thing to consider, if you directly reference any properties in your where clause from a left-joined group (using the into syntax) without checking for null, Entity Framework will still convert your LEFT JOIN into an INNER JOIN.
To avoid this, filter on the "from x in leftJoinedExtent" part of your query like so:
var y = from parent in thing
join child in subthing on parent.ID equals child.ParentID into childTemp
from childLJ in childTemp.Where(c => c.Visible == true).DefaultIfEmpty()
where parent.ID == 123
select new {
ParentID = parent.ID,
ChildID = childLJ.ID
};
ChildID in the anonymous type will be a nullable type and the query this generates will be a LEFT JOIN.

Resources