Linq Left Join not working - linq

I am trying to get this query into Linq
SELECT [ID], [Name], LastSync, Phase
FROM [Store]
LEFT JOIN (
SELECT GLog.[StoreId] AS [StoreId], LastSync, Phase
FROM [GGCSyncLog] AS GLog
INNER JOIN (
SELECT MAX(G1.[DateTime]) AS LastSync, G1.[StoreId]
FROM [GGCSyncLog] AS G1
GROUP BY G1.[StoreId]
) AS G2 ON (GLog.[StoreId] = G2.[StoreId]) AND (GLog.[DateTime] = G2.[LastSync])
) AS MostRecentLog ON Store.[ID] = MostRecentLog.[StoreId]
Its results are
ID Name LastSync Phase
1 Sarasota 2010-07-31 5
2 Wellington 2010-07-31 8
3 Tampa International 2013-03-12 8
5 Events NULL NULL
6 PO Holding Store NULL NULL
My Linq returns the correct results except I'm missing the two rows with the null LastSync & Phase. Any idea what's wrong?
from s in Stores
join gLog in
(from g1 in GGCSyncLogs.DefaultIfEmpty()
join g in
(from g in GGCSyncLogs
group g by g.StoreId into gM
select new {
StoreId = gM.Key, LastSync = gM.Max(gg=>gg.DateTime) })
on new {g1.StoreId, LastSync = g1.DateTime} equals new {g.StoreId, g.LastSync}
select new {g1.StoreId, g.LastSync, g1.Phase})
on s.ID equals gLog.StoreId
select new {s.ID, s.Name,
LastSync = (gLog != null ? (DateTime?)gLog.LastSync : null),
Phase = (gLog != null ? (int?)gLog.Phase : null) }

You should read about join clause and How to: Perform Left Outer Joins.
It's hard to get working solution without database here, but hope this will help you get back on track:
var g2 = from g1 in GGCSyncLogs
group g1 by g1.StoreId into gM;
var MostRecentLogs = from gLog in GGCSyncLogs
join g in g2 on new { gLog.StoreId, LastSync = gLog.DateTime} equals new { g.StoreId, g.LastSync }
select new { gLog.StoreId, LastSync = gLog.Date, Phase = gLog.Phase };
var results = from s in Stores
join gLog in MostRecentLogs on s.Id equals gLog.StoreId into gl
from l in gl.DefaultIfEmpty(new { LastSync = null, Phase = null })
select new {
s.Id,
s.Name,
l.LastSync,
l.Phase
};

I had to use an "into"
from s in Stores
join gRec in
(from g in GGCSyncLogs
join g2 in
(from g1 in GGCSyncLogs
group g1 by g1.StoreId into gM
select new {StoreId = gM.Key,LastSync = gM.Max(gg=>gg.DateTime)})
on new {g.StoreId, LastSync = g.DateTime} equals new {g2.StoreId, g2.LastSync}
select new {g.StoreId, g2.LastSync, g.Phase})
on s.ID equals gRec.StoreId into gRec2
from gRec3 in gRec2.DefaultIfEmpty()
select new {s.ID, s.Name,
LastSync = (gRec3 != null ? (DateTime?)gRec3.LastSync : null),
Phase = (gRec3 != null ? (int?)gRec3.Phase : null) }

Related

Which is best way to represent huge data using Web.API?

Case1:
var query = (from p in db.TblPost where (from q in db.TblThread
where q.LocationLocationid == locationID && q.CategoriesCategoryid == categoryID select q.Threadid).Contains(p.ThreadThreadid)
join r in db.TblThread on p.ThreadThreadid equals r.Threadid join s in db.TblUser on p.UserUserid equals s.Userid join t
in db.TblCategories on r.CategoriesCategoryid equals t.Categoryid join u in db.TblLocation on r.LocationLocationid
equals u.Locationid orderby r.CreatedTime descending
select new { p, r.Subject, r.EventAddress, r.EventClosetime, r.EventDate, r.EventDuration, r.EventStarttime,
r.EventTitle, r.IseventAllday, r.TargetUsers, r.CreatedTime, s.FirstName, s.MiddleName, s.LastName, t.Name,
u.Locationname, r.Isreadonly }).ToList();
OR
Case 2:
List<TblPost> _tblPost = new List<TblPost>();
_tblPost = (from p in db.TblPost select p).ToList();
List<TblThread> _tblThread = new List<TblThread>();
_tblThread = (from p in db.TblThread select p).ToList();
List<TblUser> _tblUser = new List<TblUser>();
_tblUser = (from p in db.TblUser select p).ToList();
List<TblLocation> _tblLocation = new List<TblLocation>();
_tblLocation = (from p in db.TblLocation select p).ToList();
List<TblCategories> _tblCategory = new List<TblCategories>();
_tblCategory = (from p in db.TblCategories select p).ToList();
var query = (from p in _tblPost where (from q in _tblThread where q.LocationLocationid == locationID
&& q.CategoriesCategoryid == categoryID select q.Threadid).Contains(p.ThreadThreadid) join r in _tblThread on p.ThreadThreadid
equals r.Threadid join s in _tblUser on p.UserUserid equals s.Userid join t in _tblCategory on r.CategoriesCategoryid equals
t.Categoryid join u in _tblLocation on r.LocationLocationid equals u.Locationid orderby r.CreatedTime descending
select new { p, r.Subject, r.EventAddress, r.EventClosetime, r.EventDate, r.EventDuration, r.EventStarttime,
r.EventTitle, r.IseventAllday, r.TargetUsers, r.CreatedTime, s.FirstName, s.MiddleName, s.LastName,
t.Name, u.Locationname, r.Isreadonly }).ToList();
I've these two codes which results the same. But, in case 2 i'm getting the result some quickly as compared to case 1. Which one should i need to follow?
Shall i follow case 2? is it right way to get huge data in Web.API?

LINQ Multiple table left join, distinct count not giving proper result

I am sql query us producing correct result, but when i'm doing the same in LINQ. Output is incorrect. Please let me know where my making mistake.
Following linq query that i created.
LINQ Query:
List<UserModel> Model = (from users in db.UserM
join ct in db.CustT on users.UserId equals ct.UserID into group1
from g1 in group1.DefaultIfEmpty()
join ti in db.TestIn on g1.TestId equals ti.TestID into group2
from g2 in group2.DefaultIfEmpty()
where (users.CustomerId==CustomerId) && (users.RoleId == 4) && (users.Status == 1)
group new
{
g2.TestInvitationID,
g2.TestID,
}
by new
{
users.FirstName,
users.CreatedOn,
users.Email,
users.UserId
} into group4
select new UserModel
{
Name = group4.Key.FirstName,
CreatedOn = group4.Key.CreatedOn,
EmailId = group4.Key.Email,
UserId = group4.Key.UserId,
NoOfTestTaken = group4.Select(x=>x.TestID).Distinct().Count(),
NoOfInvitationsSent = group4.Count(x => x.TestInvitationID != 0)
}).ToList();
SQL Query:
SELECT IsNull(COUNT(distinct TS.TestId),0) AS NoOfTests,
IsNull(COUNT(TS.TestInvitationID),0) AS NoOfInvitations,
UM.Email,
UM.UserId,
UM.FirstName,
UM.CreatedOn
FROM UserM as UM
left JOIN CustT AS CT
ON UM.UserId=CT.UserId
left JOIN TestIn AS TS
ON TS.TestId = CT.TestId
WHERE UM.CustomerId=41
AND UM.RoleId=4
and UM.[Status]=1
GROUP BY UM.UserId, UM.Email, UM.FirstName, UM.CreatedOn
Tables:
"UserM" - columns: UserId, Email, FirstName, CreatedOn
"CustT" - columns: TestId, UserId,
"TestIn" - columns: TestInvitationId, TestId
The difference between SQL COUNT(expr) and LINQ Count is that the former excludes NULL values, which produces a difference when used on right side of a left outer join with no matching records (SQL will produce 0 while LINQ 1). The closest LINQ equivalent is Count(expr != null).
So the direct translation of your SQL query would be like this (note that the generated SQL query could and most likely will be different):
(A side note: When converting SQL query to LINQ, it's good to use the same aliases to make it easier to see the mappings)
var query =
from um in db.UserMasters
join ct in db.CustTests on um.UserId equals ct.UserID
into ctGroup from ct in ctGroup.DefaultIfEmpty() // left outer join
join ts in db.TestInvitaions on ct.TestId equals ts.TestID
into tsGroup from ts in tsGroup.DefaultIfEmpty() // left outer join
where um.CustomerId == UserSession.CustomerId
&& um.RoleId == 4
&& um.Status == 1
group ts by new { um.UserId, um.Email, um.FirstName, um.CreatedOn } into g
select new UserModel
{
Name = g.Key.FirstName,
CreatedOn = g.Key.CreatedOn,
EmailId = g.Key.Email,
UserId = g.Key.UserId,
NoOfTestTaken = g.Where(ts => ts != null).Select(ts => ts.TestID).Distinct().Count(),
NoOfInvitationsSent = g.Count(ts => ts != null)
};
var result = query.ToList();
I suspect that the following row is the problem because is not the same like in your sql:
Linq:
NoOfInvitationsSent = group4.Count(x => x.TestInvitationID != 0)
SQL:
IsNull(COUNT(TS.TestInvitationID),0) AS NoOfInvitations
Due to counting items from a left join Linq should be instead:
NoOfInvitationsSent = group4.Where(i => i != null).Count()
To put it all together, with a bit of better formatting:
var model = (from users in db.UserMasters
join ct in db.CustTests on users.UserId equals ct.UserID into group1
from ct in group1.DefaultIfEmpty()
join ti in db.TestInvitaions on ct.TestId equals ti.TestID into group2
from ct in group2.DefaultIfEmpty()
where users.CustomerId == UserSession.CustomerId &&
users.RoleId == 4 &&
users.Status == 1
group new { ct.TestInvitationID, ct.TestID }
by new
{
users.FirstName,
users.CreatedOn,
users.Email,
users.UserId
} into grouping
select new UserModel
{
Name = grouping.Key.FirstName,
CreatedOn = grouping.Key.CreatedOn,
EmailId = grouping.Key.Email,
UserId = grouping.Key.UserId,
NoOfTestTaken = grouping.Where(i => i != null).Select(x => x.TestID).Distinct().Count(),
NoOfInvitationsSent = grouping.Where(i => i != null).Count()
}).ToList();

How to select first row of each id linq

So I have few tables and i want inner join it information two create
new object. But I have a little bit trouble.
One my table have connection one to many, and when linq request , it
give me more result than i want , he just copy information. I need
request something like this:
IPagedList<HelperListings> srch = (from l in db.gp_listing
where l.DateCreated > weekago
join lp in db.gp_listing_photo on l.Id equals lp.ListingId
join loc in db.gp_location on l.LocationId equals loc.Id
orderby l.DateCreated ascending
select new HelperListings { id = l.Id, HouseNumber = l.HouseNumber,ListingPrice = l.ListingPrice, PhotoUrl = lp.PhotoUrl.First(), AreaStateCode = loc.AreaStateCode }).ToList().ToPagedList(page ?? 1, 15);
PhotoUrl = lp.PhotoUrl.First() i need something like this but i don`t have any ideas how to do it. Need ur help guys.
UPDATE :
Responding to your comment, you have at least 2 options : 1. use group by and select only the first PhotoUrl from each group, or 2. don't join to gp_listing_photo table to avoid duplicated rows, and use subquery to get only the first PhotoUrl. Example for the latter :
IPagedList<HelperListings> srch =
(from l in db.gp_listing
where l.DateCreated > weekago
join loc in db.gp_location on l.LocationId equals loc.Id
orderby l.DateCreated ascending
select new HelperListings
{
id = l.Id,
HouseNumber = l.HouseNumber,
ListingPrice = l.ListingPrice,
PhotoUrl = (from lp in db.gp_listing_photo where l.Id = lp.ListingId select lp.PhotoUrl).FirstOrDefault(),
AreaStateCode = loc.AreaStateCode
}
).ToList().ToPagedList(page ?? 1, 15);
How about simply appending .Distinct() after your LINQ query to avoid duplicated data :
IPagedList<HelperListings> srch =
(from l in db.gp_listing
where l.DateCreated > weekago
join lp in db.gp_listing_photo on l.Id equals lp.ListingId
join loc in db.gp_location on l.LocationId equals loc.Id
orderby l.DateCreated ascending
select new HelperListings
{
id = l.Id,
HouseNumber = l.HouseNumber,
ListingPrice = l.ListingPrice,
PhotoUrl = lp.PhotoUrl,
AreaStateCode = loc.AreaStateCode
}
).Distinct().ToList().ToPagedList(page ?? 1, 15);
For Reference : LINQ Select Distinct with Anonymous Types

linq to entities left outer join

select SF.FOLDER_NAME,SF.CREATED_DATE,COUNT(st.FOLDER_ID)
from SURVEY_FOLDER SF with (nolock) left outer join SURVEY_TEMPLATE ST with (nolock)
on SF.FOLDER_ID=ST.FOLDER_ID
group by SF.FOLDER_NAME,SF.CREATED_DATE
I need this query in Linq :
I have tried this query,but unable to group by.
My Linq Query :
var data = (from xx in VDC.SURVEY_FOLDER
join yy in VDC.SURVEY_TEMPLATE
on xx.FOLDER_ID equals yy.FOLDER_ID into g
from grt in g.DefaultIfEmpty()
select
new
{
xx.FOLDER_NAME,
xx.CREATED_DATE,
count = g.Count()
}).ToList();
I got the Answer :
var data5 = (from SF in VDC.SURVEY_FOLDER
join ST in VDC.SURVEY_TEMPLATE on new { FOLDER_ID = SF.FOLDER_ID } equals new { FOLDER_ID = (Int64)ST.FOLDER_ID } into ST_join
from ST in ST_join.DefaultIfEmpty()
group new { SF, ST } by new
{
SF.FOLDER_NAME,
SF.CREATED_DATE
} into g
select new
{
g.Key.FOLDER_NAME,
CREATED_DATE = (DateTime?)g.Key.CREATED_DATE,
Column1 = (Int64?)g.Count(p => p.ST.FOLDER_ID != null)
}).ToList();

Use of where in multiple joins to remove rows - linq

I have a table of orders. the status is on the soilorders which is joined to the orders.
I only want to return orders where the joined soilorder does not have status "Removed".
I had thought that
join sso in db.SoilSamplingOrders on ord.order_id equals sso.order_id
where sso.status.Equals("Removed")!=true
but then no records are returned!
thanks for any help (query below)
var query =
from ord in db.Orders
join sso in db.SoilSamplingOrders on ord.order_id equals sso.order_id
where sso.status.Equals("Removed")!=true
join cust in db.Customers on ord.customer_id equals cust.customer_id
select new Listing
{
assigned_to = sso.assigned_to,
company = cust.company,
order_id = ord.order_id,
order_created = ord.order_created,
customer_id = ord.customer_id,
order_created_by_employ_id = ord.order_created_by_employ_id,
first_farm_on_order = (from f in db.SoilSamplingSubJobs
where f.order_id == ord.order_id
select new ListingSubJob { first_farm_on_order = f.farm }).
AsEnumerable().First().first_farm_on_order,
total_fields = (from f in db.SoilSamplingSubJobs
where f.order_id == ord.order_id
select new { f.sssj_id }).AsEnumerable().Count(),
total_area = (float?) (from f in db.SoilSamplingSubJobs
where f.order_id == ord.order_id && f.area_ha != null
select f.area_ha ).Sum() ?? 0 ,
total_area_ph_density = (float?)(from f in db.SoilSamplingSubJobs
where f.order_id == ord.order_id && f.ph != null
select f.ph).Sum() ?? 0,
};
DOH! Just as nature abhors a vacuum, anything Null cannot be included in the select. Added values to the status field and bom it works.

Resources