how to do left join in lambda - linq

var item= (from t1 in _dbEntities.PurchaseSales
join t2 in _dbEntities.ItemTypes on t1.ItemTypeID equals t2.ID
where t2.ID.Equals(null)
select t2).ToList();
how to do left join in it

Use DefaultIfEmpty:
var item= (from t1 in _dbEntities.PurchaseSales
join t2 in _dbEntities.ItemTypes on t1.ItemTypeID equals t2.ID into t
from l in t.DefaultIfEmpty()
where l == null
select t1).ToList();

Related

How do you write an INNER JOIN with an "OR" in Linq

I am trying to write a Linq query to generate the following SQL
SELECT
[t1].[Id], [t2].[value3]
FROM
[Table1] AS [t1]
INNER JOIN
[Table2] AS [t2] ON [t1].[Id] = [t2].[value1]
OR [t1].[Id] = [t2].[value2]
I have seen lots of examples for how to do multiple joins, but none for how to do this type of "one or the other" join.
var result = from t1 in context.Table1
from t2 in context.Table2
where (t1.Id == t2.value1 || t1.Id == t2.value2)
select new
{
t1.Id,
t2.value3
};
INNER JOIN
var query =
from t1 in context.Table1
from t2 in context.Table2.Where(t2 => t1.Id == t2.value1 || t1.Id == t2.value2)
select new
{
t1.Id,
t2.value3
};
LEFT JOIN
var query =
from t1 in context.Table1
from t2 in context.Table2.Where(t2 => t1.Id == t2.value1 || t1.Id == t2.value2)
.DefaultIfEmpty()
select new
{
t1.Id,
t2.value3
};

How to LEFT OUTER JOIN on specific column values in HIVEQL?

When I run the following HiveQL code I get the error:
Execute error: Error while compiling statement: FAILED: SemanticException [Error 10004]: Line 1:2112 Invalid table alias or
column reference 'T3'
SELECT *
FROM CC_CLAIM_EXT T1
INNER JOIN CC_EXPOSURE_EXT T2 ON (T1.ID = T2.CLAIMID)
LEFT OUTER JOIN CC_POLICY_EXT T3 ON (T1.POLICYID = T3.ID)
LEFT OUTER JOIN CC_COVERAGE_EXT T4 ON (T2.COVERAGEID = T4.ID)
LEFT OUTER JOIN CC_TRANSACTION_EXT T5 ON (T2.ID = T5.EXPOSUREID)
LEFT OUTER JOIN CC_TRANSACTIONSET_EXT T6 ON (T5.TRANSACTIONSETID = T6.ID)
LEFT OUTER JOIN CC_TRANSACTIONLINEITEM_EXT T7 ON (T5.ID = T7.TRANSACTIONID)
LEFT OUTER JOIN CC_RISKUNIT_EXT T12 ON (T4.RISKUNITID = T12.ID)
LEFT OUTER JOIN CC_CLASSCODE_EXT T13 ON (T12.CLASSCODEID = T13.ID)
LEFT OUTER JOIN (SELECT TT12.CLAIMID
,CASE WHEN COUNT(TT13.PRIMARYBODYPART) > 1 THEN 10010 ELSE MAX(TT13.PRIMARYBODYPART) END AS PRIMARYBODYPART
,CASE WHEN COUNT(TT13.DETAILEDBODYPART) > 1 THEN 10010 ELSE MAX(TT13.DETAILEDBODYPART) END AS DETAILEDBODYPART
FROM CC_INCIDENT_EXT TT12
LEFT OUTER JOIN CC_BODYPART_EXT TT13 ON (TT12.ID = TT13.INCIDENTID)
GROUP BY TT12.CLAIMID) T14
ON (T1.ID = T14.CLAIMID AND T3.POLICYTYPE IN(10022,10023))
WHERE T1.STATE IN(2,3)
AND T2.STATE IN(2,3)
AND T6.APPROVALSTATUS = 1
AND T7.RETIRED = 0
ORDER BY CLAIMNUMBER
,EXPOSUREID
,TRANSACTIONID
I've narrowed it down to the line:
ON (T1.ID = T14.CLAIMID AND T3.POLICYTYPE IN(10022,10023))
If I delete:
AND T3.POLICYTYPE IN(10022,10023)
The code runs fine. Is there a better way to limit this join in HiveQL?
The error is because you put a reference on the left join between T1 and T14 BUT you put T3 on the condition. To limit your query to your specified T3.policy_id, you should put in along the line where you left join T1 and T3. See below in line 4:
SELECT *
FROM CC_CLAIM_EXT T1
INNER JOIN CC_EXPOSURE_EXT T2 ON (T1.ID = T2.CLAIMID)
LEFT OUTER JOIN CC_POLICY_EXT T3 ON (T1.POLICYID = T3.ID AND T3.POLICYTYPE IN(10022,10023))
LEFT OUTER JOIN CC_COVERAGE_EXT T4 ON (T2.COVERAGEID = T4.ID)
LEFT OUTER JOIN CC_TRANSACTION_EXT T5 ON (T2.ID = T5.EXPOSUREID)
LEFT OUTER JOIN CC_TRANSACTIONSET_EXT T6 ON (T5.TRANSACTIONSETID = T6.ID)
LEFT OUTER JOIN CC_TRANSACTIONLINEITEM_EXT T7 ON (T5.ID = T7.TRANSACTIONID)
LEFT OUTER JOIN CC_RISKUNIT_EXT T12 ON (T4.RISKUNITID = T12.ID)
LEFT OUTER JOIN CC_CLASSCODE_EXT T13 ON (T12.CLASSCODEID = T13.ID)
LEFT OUTER JOIN (SELECT TT12.CLAIMID
,CASE WHEN COUNT(TT13.PRIMARYBODYPART) > 1 THEN 10010 ELSE MAX(TT13.PRIMARYBODYPART) END AS PRIMARYBODYPART
,CASE WHEN COUNT(TT13.DETAILEDBODYPART) > 1 THEN 10010 ELSE MAX(TT13.DETAILEDBODYPART) END AS DETAILEDBODYPART
FROM CC_INCIDENT_EXT TT12
LEFT OUTER JOIN CC_BODYPART_EXT TT13 ON (TT12.ID = TT13.INCIDENTID)
GROUP BY TT12.CLAIMID) T14
ON T1.ID = T14.CLAIMID
WHERE T1.STATE IN(2,3)
AND T2.STATE IN(2,3)
AND T6.APPROVALSTATUS = 1
AND T7.RETIRED = 0
ORDER BY CLAIMNUMBER
,EXPOSUREID
,TRANSACTIONID

Doing a Left outer join in a linq query

I have the query below that works in sqllite with a left outer join
select * from customer cu
inner join contract cnt on cu.customerId = cnt.customerid
inner join address addy on cu.addressid = addy.addressId
inner join csrAssoc cassc on cu.customerid = cassc.customerId
left outer join CustomerServiceRepresentative csrr on cassc.csrid = csrr.customerservicerepresentativeId
inner join customerServiceManager csmm on cassc.csmid = csmm.customerservicemanagerId
where cu.customernumber = '22222234'
I want to be able to apply a left outer join on this line in the linq query below
join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals
csrr.CustomerServiceRepresentativeId
VisitRepData = (from cu in objCustomer.AsEnumerable()
join cnt in objContract.AsEnumerable() on cu.customerId equals cnt.customerId
join addy in objAddress.AsEnumerable() on cu.addressId equals addy.addressId
join cassc in objCsrAssoc.AsEnumerable() on cu.customerId equals cassc.CustomerId
join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals
csrr.CustomerServiceRepresentativeId
join csmm in objCustServMan on cassc.CsmId.ToString() equals csmm.customerServiceManagerId
where cu.CustomerNumber == (customernbr)
How can I do a left outer join in a linq query?
Here is my comment after adjusting and running the code. The other section is also added. All am getting is object is not set to an instance of an object.
var VisitRepData = from cu in objCustomer.AsEnumerable()
join cnt in objContract.AsEnumerable() on cu.customerId equals cnt.customerId
join addy in objAddress.AsEnumerable() on cu.addressId equals addy.addressId
join cassc in objCsrAssoc.AsEnumerable() on cu.customerId equals cassc.CustomerId
join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals
csrr.CustomerServiceRepresentativeId into temp
from tempItem in temp.DefaultIfEmpty()
join csmm in objCustServMan on cassc.CsmId.ToString() equals csmm.customerServiceManagerId
where cu.CustomerNumber == (customernbr)
select new
{
cu.customerId,
cu.CustomerNumber,
cu.customerName,
cu.dateActive,
cnt.contractExpirationDate,
addy.street,
addy.street2,
addy.city,
addy.state,
addy.zipcode,
cu.EMail,
cu.phoneNo,
cu.faxNumber,
csmm.customerServiceManagerName,
tempItem.CustomerServiceRepresentativeName,
};
foreach (var item in VisitRepData)
{
var one = item.customerId;
var two = item.CustomerNumber;
}
Per documentation:
A left outer join is a join in which each element of the first collection is returned, regardless of whether it has any correlated elements in the second collection. You can use LINQ to perform a left outer join by calling the DefaultIfEmpty method on the results of a group join
(emphasis mine)
Based on MSDN link above, and if I understood your requirements correctly, query should look like this:
VisitRepData = from cu in objCustomer.AsEnumerable()
join cnt in objContract.AsEnumerable() on cu.customerId equals cnt.customerId
join addy in objAddress.AsEnumerable() on cu.addressId equals addy.addressId
join cassc in objCsrAssoc.AsEnumerable() on cu.customerId equals cassc.CustomerId
join csrr in objCsrCustServRep.AsEnumerable() on cassc.CsrId equals
csrr.CustomerServiceRepresentativeId into temp
from tempItem in temp.DefaultIfEmpty()
join csmm in objCustServMan on cassc.CsmId.ToString() equals csmm.customerServiceManagerId
where cu.CustomerNumber == (customernbr)
Specifically, the left outer join is performed with this code:
join csrr in objCsrCustServRep.AsEnumerable()
on cassc.CsrId equals csrr.CustomerServiceRepresentativeId
into temp
from tempItem in temp.DefaultIfEmpty()

Want to change SQL query to LINQ

I want to change the below simple SQL query into LINQ , how do I change it ?
select * from table1 where isPaid = 'true' and Id in (select Id from table2 where EmployeeId = 12)
similar to this ?
from pa in db.PaymentAdvices
where pa.IsPaid == true
orderby pa.PaidDate descending
select pa;
Here code linq to sql:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12
select t1
Hope usefull!
if the field isPaid has datatype is Boolean:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid == true
select t1
if the field isPaid has datatype is String:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid.Equals("true")
select t1

Inner join in Linq with more than 2 datatables

i have 3 tables
t1==>
t1.ID t1.co1 t1.col2
1 a b
2 a b
t2==>
t2.ID t2.co1 t2.col2
1 a b
2 a b
t3==>
t3.ID t3.co1 t3.col2
1 a b
2 a b
i want inner join between all three tables using Linq and want selected column in 4th datatable.
equivalent sql query:
SELECT t1.ID,t2.col1,t3.col2
FROM t1
INNER JOIN t2 ON t1.ID=t2.ID
INNER JOIN t3 ON t1.ID=t3.ID
t4==>
t1.ID t2.co1 t3.col2
1 a b
2 a b
Something like this
var Result =
from row1 in t1
join row2 in t2 on row1.ID equals row2.ID
join row3 in t3 on row1.ID equals row3.ID
select new { ID = row1.ID, Col1 = row2.col1, Col2 = row3.col2 }
DataTable dt = Result.CopyToDataTable();
Use LoadDataRow() to get a DataTable from an anonymous type as here. Else Result.CopyToDataTable().
//Get the column list as same as table1 to new datatable
DataTable table4 = table1.Clone();
var Result =
from x in t1.AsEnumerable() join
y in t2.AsEnumerable() on x.Field<int>("ID") equals y.Field<int>("ID") join
z in t3.AsEnumerable() on x.Field<int>("ID") equals z.Field<int>("ID")
select new table4.LoadDataRow(
new object[] {
x.ID,
y.col1,
z.col2
}, false);

Resources