I have a normally simple looking query in SQL as:
SELECT table1.Id, count(table2.col) AS OrderCol
FROM table1
LEFT JOIN table2 ON table1.Id = table2.Id
LEFT JOIN table3 ON table2.Id = table3.Id AND table2.condition = 3 //some integer value
GROUP BY table1.Id
ORDER BY count(table2.col) DESC
When AND clause appears inside join, I am not sure about how to convert this to LINQ...
How to achieve it?
It goes something like:
from t1 in db.Table1
join t2 in db.Table2 on t1.field equals t2.field
select new { t1.field2, t2.field3}
Try this:
var answer = (from t1 in table1
join t2 in table2 on t1.Id equals t2.Id into subData1
from t2sub in subData1.DefaultIfEmpty()
join t3 in table3 on new { Id = t2sub == null ? 0 : t2sub.Id, condition = t2sub == null ? 0 : t2sub.condition } equals new { t3.Id, condition = 3 } into subData
from t3sub in subData.DefaultIfEmpty()
group new { t1, t2sub } by t1.Id into subGroup
orderby subGroup.Count(x => x.t2sub != null) descending
select new {
Id = subGroup.Key,
OrderCol = subGroup.Count(x => x.t2sub != null)
}).ToList();
Related
var allData = (from t1 in table1
join t2 in table2
on t1.Column1 equals t2.Column1
join t3 in table3
on t1.Column1.ToString() equals t3.Column1
join t4 in table4
on t3.Column1 equals t4.Column1
join t6 in table4
on t1.Column1 equals t6.Column1
join t5 in table5
on new { X1 = t4.Column1, X2 = t4.Column2 }
equals new { X1 = t5.Column1, X2 = t5.Column1 }
i have the same table table4 twice but how can i use equals from different table just like what I did for table5?
You can use a Where clause if you don't want to specify table4 twice:
var allData = (from t1 in table1
join t2 in table2
on t1.Column1 equals t2.Column1
join t3 in table3
on t1.Column1.ToString() equals t3.Column1
join t4 in table4
on t3.Column1 equals t4.Column1
join t5 in table5
on new { X1 = t4.Column1, X2 = t4.Column2 }
equals new { X1 = t5.Column1, X2 = t5.Column1 }
where t1.Column1 == t4.Column1
select ....
I have done an inner join, it shows only the matching record with following query :-
var data = from t1 in ctx.tblEmp
join t2 in ctx.tblHelp
on t1.Field equals t2.Fld
where t1.Id == Id &&
t2.Id == Id
select new Settings { Master = t1.Labelname, OrderNo = t2.OrderNo};
I want all the records from tblEmp & only matching records from tblHelp.
How to do this?
Use as
var data = from t1 in ctx.tblEmp
join t2 in ctx.tblHelp
on t1.Field equals t2.Fld into u
from t2 in u.DefaultIfEmpty()
where t1.Id == Id &&
t2.Id == Id
select new Settings { Master = t1.Labelname, OrderNo = t2.OrderNo};
Try this
var data = from t1 in ctx.tblEmp
join t2 in ctx.tblHelp
on t1.Field equals t2.Fld into u
from t2 in u.DefaultIfEmpty()
where t1.Id == Id
orderby columnname // Added Order By
select new Settings { Master = t1.Labelname, OrderNo = t2.OrderNo==null ?"":t2.OrderNo};
var query = from t1 in ctx.tblEmp
join t2 in ctx.tblHelp on t1 equals t2.Fld into tempGroup
from subpet in tempGroup.DefaultIfEmpty()
select new { t1.Labelname, OrderNo = (subpet == null ? String.Empty : subpet.OrderNo) };
I cannot make the following query into LINQ expression. Could you help me ?
SELECT Employee_Id_FK from TimeRegistrations as tr
JOIN Employees em ON tr.Employee_Id_FK = em.id
JOIN Managers m ON em.Manager_Id_FK = m.id
WHERE m.id = 4
This is what I have so far :
var result = from t in DB.TimeRegistrations
join Employees in DB.TimeRegistrations on t.Employee_Id_FK equals Employees.id
join Managers in DB.Managers on ..... ;
// Display results.
foreach (var r in result)
{
Console.WriteLine(r);
}
In case your db has foreign keys:
var result =from t1 in DB.TimeRegistrations
join t2 in t1.Employees
join t3 in t2.Managers
where t3.id == "4"
select t1
in case it does not:
var result =from t1 in DB.TimeRegistrations
join t2 in DB.Employees on t1.Employee_Id_FK equals t2.id
join t3 in DB.Managers on t2.id equals t3.Manager_Id_FK
where t3.id == "4"
select t1
However shorter way will be:
var result = DB.Managers.Find(4).Employees.SelectMany(e=>e.TimeRegistrations)
Or not that straight forward way:
var result = DB.TimeRegistrations
.Where(t=>t.Employees.Any(e=>e.Managers.Any(m=>m.id == 4)))
This is my query:
var results = from table1 in dtSplitDates.AsEnumerable()
join table2 in dtSplitDates.AsEnumerable() on (int)table1["FID"] equals (int)table2["FID"] into lj
from r in lj.DefaultIfEmpty()
select dtSplitDates2.LoadDataRow(new object[]
{
r["FID"],
r["SLNO"],
r == null ? string.Empty : r["Dates"]
}, false);
Currently i am joining on Column FID - due to which i am getting 36 records (duplicates):
However in order to avoid duplicates i need to join also on SLNO column but i am unable to write that query - please help.
As per my understanding you want two join condition; Try this
var results = from table1 in dtSplitDates.AsEnumerable()
join table2 in dtSplitDates.AsEnumerable()
on new {id1 =(int)table1["FID"], SLno1= (int)table1["SLNO"]}
equals new {id2=(int)table2["FID"], SLno2=(int)table2["SLNO"]} into lj
from r in lj.DefaultIfEmpty()
select dtSplitDates2.LoadDataRow(new object[]
{
r["FID"],
r["SLNO"],
r == null ? string.Empty : r["Dates"]
}, false);
Try to implement with this example:
For Multiple Joins:
var result=(from com in db.Company.AsEnumerable()
join c in db.Country.AsEnumerable() on com.CountryID equals c.CountryID
join s in db.State.AsEnumerable() on com.StateID equals s.StateID
join ct in db.City.AsEnumerable() on com.CityID equals ct.CityID
orderby com.Name
select new CompanyModel()
{
CompanyID = com.CompanyID,
Name = com.Name,
AddressLine1 = com.AddressLine1,
CountryID = com.CountryID,
StateID = com.StateID,
CityID = com.CityID,
Country = c.CountryID,
State = s.StateID,
City = ct.CityID,
Pin = com.Pin,
Phone = com.Phone,
}).Distinct().ToList();
I am trying to perform the following SQL using LINQ and the closest I got was doing cross joins and sum calculations. I know there has to be a better way to write it so I am turning to the stack team for help.
SELECT T1.Column1, T1.Column2, SUM(T3.Column1) AS Amount
FROM T1
INNER JOIN T2
ON T1.T1ID = T2.T1ID
INNER JOIN T3
ON T2.T3ID = T3.T3ID
GROUP BY T1.Column1, T1.Column2
What I have been trying is the following LINQ code
var qTotal = from T2 in context.T2
from T3 in context.T3
where T3.T3ID == T3.T3ID
group T3 by T2.T1ID into gT2T3
from T1 in context.T1
where gT2T3.Key.Equals(T1.T1ID)
select new { T1.Column1,T1.Column2,Amount = gT2T3.Sum(t => t.Column1)};
I know there has to be a better way to write it, I just do not know how, any help would be great!
Try this:
var total = from T1 in context.T1
join T2 in context.T2 on T1.T2ID equals T2.T2ID
join T3 in context.T3 on T2.T3ID equals T3.T3ID
group T3 by new { T1.Column1, T1.Column2 } into g
select new {
Column1 = T1.Column1,
Column2 = T2.Column2,
Amount = g.Sum(t3 => t3.Column1)
};
For me (using 4.0), the following works.
var total = from T1 in context.T1
join T2 in context.T2 on T1.T2ID equals T2.T2ID
join T3 in context.T3 on T2.T3ID equals T3.T3ID
group T3 by new { T1.Column1, T1.Column2 } into g
select new {
Column1 = g.Key.Column1,
Column2 = g.Key.Column2,
Amount = g.Sum(t3 => t3.Column1)
};
Below code is working for me :
var credit = (from bm in BulkMessage
join sms in SMS on bm.BulkMessageId equals sms.BulkMessageId
where bm.ProfileId == pid && bm.IsActive == true
group sms by sms.SMSCredit into g
select new { SMSCredits = g.Sum(s => s.SMSCredit) }).FirstOrDefault();