I am trying to select all they keys after i perform a group-by. It works if I put the group into a variable then perform the select on the variable, but not directly after the group. I don't know why.
This does not work:
var pureUsers = from u in userContext.Users
join ur in userContext.UserRoles on u.UserID equals ur.UserID
join r in userContext.Roles on ur.RoleID equals r.RoleID
join sm in userContext.SystemMasters on r.SystemMasterID equals sm.SystemMasterID
where sm.SystemName == "PURE"
group ur.UserRoleID by u.SAPID into g
select g.Key;
It's giving me the error, "'string' does not contain a definition for 'Key' and no extension method 'Key' accepting a first argument of type 'string' could be found"
However, it works like this:
var pureUsers = from u in userContext.Users
join ur in userContext.UserRoles on u.UserID equals ur.UserID
join r in userContext.Roles on ur.RoleID equals r.RoleID
join sm in userContext.SystemMasters on r.SystemMasterID equals sm.SystemMasterID
where sm.SystemName == "PURE"
group ur.UserRoleID by u.SAPID
var happy = pureUsers.Select(x => x.Key);
I also tried:
var pureUsers = (from u in userContext.Users
join ur in userContext.UserRoles on u.UserID equals ur.UserID
join r in userContext.Roles on ur.RoleID equals r.RoleID
join sm in userContext.SystemMasters on r.SystemMasterID equals sm.SystemMasterID
where sm.SystemName == "PURE"
group ur.UserRoleID by u.SAPID).Select(x => x.Key);
It gives me the same error. I don't see how this is any different than putting it into pureUsers then doing the select. Also, if there is a better way of doing this that doesn't involve a group-by, please tell me. I group it by SAPID because Users can have many UserRoles and I want a list of Users.SAPID where each SAPID is displayed once.
How about:
var pureUsers = (from u in userContext.Users
join ur in userContext.UserRoles on u.UserID equals ur.UserID
join r in userContext.Roles on ur.RoleID equals r.RoleID
join sm in userContext.SystemMasters on r.SystemMasterID equals sm.SystemMasterID
where sm.SystemName == "PURE"
select u.SAPID).Distinct();
Related
There is a table I want to join with different columns in different tables.
This is how so far I did this
var purchData = (from a in db.AppRequest
join e in db.Employee on a.Req_By equals e.Id
join c in db.Company on e.CompanyId equals c.Id
join d in db.Designation on e.DesignId equals d.Id
join l in db.Master_Locations on a.Req_Location equals l.Id
join dep in db.Department on e.DepId equals dep.Id
join p in db.Purchase on a.Id equals p.Req_Id
join pi in db.PurchasingItems on p.Id equals pi.Purchase_Id
join pd in db.PurchasingDetails on p.Id equals pd.Purchase_Id
join pds in db.PurchasingDetailsSup on pd.Id equals pds.PurchasingDetails_Id
join s in db.M_Supplier on pds.Supp_Id equals s.Id
join payMethod in db.Master_PayMethods on s.Pay_Method equals payMethod.Id
join poNo in db.ApprovedPoNumbers on p.Id equals poNo.Purchase_Id
where a.Id == id && pds.IsApproved == true
In db.ApprovedPoNumbers table has purchase_Id and Supplier_Id
In db.PurchasingDetailsSup table has purchase_Id and Supplier_Id
So I want to know that here join poNo in db.ApprovedPoNumbers on p.Id equals poNo.Purchase_Id line I want to join the db.ApprovedPoNumbers table purchase_Id,Supplier_Id with db.PurchasingDetailsSup table purchase_Id and Supplier_Id
You can achieve that by building two objects with the criterias you intend to match and use the equals operator on them:
on new {poNo.purchase_Id, poNo.Supplied_Id} equals new {pds.purchase_Id, pds.Supplier_Id} into details
When allUserDepart is null other join fire a exception on line of join allDepartment. Because uD its null. How to fix this code?
join d in allDepartment on uD.DepartmentId equals d.DepartmentId into departmentJoin
--Ful Code is
var data = (from us in allUsers
join uR in allUserRoles on us.UserId equals uR.UserId into userRoleJoin
from uR in userRoleJoin.DefaultIfEmpty()
join r in allRoles on uR.RoleId equals r.RoleId
join uD in allUserDepartment on us.UserId equals uD.NewUserId into userDepJoin
from uD in userDepJoin.DefaultIfEmpty()
join d in allDepartment on uD.DepartmentId equals d.DepartmentId into departmentJoin
from d in departmentJoin.DefaultIfEmpty()
I am trying to join three SQL tables in LINQ c# for the below SQL
SELECT
rpp.*
FROM dbo.Orgs ao
LEFT JOIN dbo.Afflia rpa
ON rpa.AccountId = ao.ID
INNER JOIN dbo.reports rpp
ON rpp.Id = rpa.reporttId
WHERE ao.Name like '%xyz%'
above query returns data but the equivalent LINQ query doesn't as in below
from a in context.Orgs
join aff in context.Afflia on a.ID equals aff.AccountId
join prescriber in context.Reports on aff.reportId equals prescriber.Id
where a.ORG_NAME.Contains("xyz")
May I know where the mistake is?
In LINQ you did INNER join but In SQL, you did LEFT join.
Try this instead:
from a in context.Orgs
join aff in context.Afflia on a.ID equals aff.AccountId into affs
from aff in affs.DefaultIfEmpty()
join prescriber in context.Reports on aff.reportId equals prescriber.Id
where a.ORG_NAME.Contains("xyz")
In your SQL you are doing a LEFT join to dbo.Afflia, but in your LINQ you are doing an inner join. You need to add "DefaultIfEmpty(), eg
from aff in context.Afflia.Where(join condition here).DefaultIfEmpty()
You could do:
var prescribers = (from a in context.Orgs
from aff in context.Afflia.Where(aff => aff.AccountId == a.ID)
from prescriber in context.Reports.Where(pres => pres.Id == aff.reportId)
where a.ORG_NAME.Contains("xyz")
select prescriber)
.ToList();
I'm sorry for telling that I've a little bit weak on LINQ, I always do write SQL query before I start working on the complicated LINQ.
I want to ask that how to convert this SQL Query into LINQ with LEFT JOIN with multiple ON conditons with the OR operator.,
m.MerchandiseId will be use for twice in ON condition
SELECT
*
FROM
Inbox AS i
INNER JOIN [User] AS u ON i.FromUserId = u.UserId
LEFT OUTER JOIN Merchandise AS m ON
u.MerchandiseId = m.MerchandiseId
OR
i.ToMerchantId = m.MerchandiseId
WHERE
i.ToCompanyId = 10
OR
i.FromCompanyId = 10
var message = (from i in db.Inbox
join u in db.User on i.FromUserId equals u.UserId
join m in db.Merchandise on u.MerchandiseId equals m.MerchandiseId //here I want to ON i.MerchantId = m.MerchandiseId, but it doesn't allow
where i.ToCompanyId == user.CompanyId || i.FromCompanyId == user.CompanyId
orderby i.CreatedAt descending
group m.MerchandiseId by new { m.MerchandiseId, m.MerchandiseName } into grp
select new
{
MerchandiseId = grp.Key.MerchandiseId,
MerchandiseName = grp.Key.MerchandiseName,
InboxMessage = (from e in db.Inbox
join us in db.User on e.FromUserId equals us.UserId
join mer in db.Merchandise on us.MerchandiseId equals mer.MerchandiseId
where mer.MerchandiseId == grp.Key.MerchandiseId
orderby e.CreatedAt descending
select e.InboxMessage).FirstOrDefault(),
CreatedAt = (from e in db.Inbox
join us in db.User on e.FromUserId equals us.UserId
join mer in db.Merchandise on us.MerchandiseId equals mer.MerchandiseId
where mer.MerchandiseId == grp.Key.MerchandiseId
orderby e.CreatedAt descending
select e.CreatedAt).FirstOrDefault(),
}).ToList();
The bottom LINQ Query I've write for it. However, I just can work on the left join with multiple ON clause in LINQ. Appreciate if someone would help me on this. Thanks!
I don't believe Linq supports the use of the OR operator with multiple columns, but that said, I wouldn't use OR even in SQL as it makes the join's intention unclear and it also obscures where the data originated from - it also isn't immediately clear what happens if there are multiple matches for each column. Instead I would JOIN twice on the different columns and let the projection-clause handle it:
SELECT
*
FROM
Inbox
INNER JOIN [User] AS u ON i.FromUserId = u.UserId
LEFT OUTER JOIN Merchandise AS userMerchant ON u.MerchandiseId = userMerchant.MerchandiseId
LEFT OUTER JOIN Merchandise AS inboxMerchant ON Inbox.ToMerchantId = inboxMerchant .MerchandizeId
WHERE
Inbox.ToCompanyId = 10
OR
Inbox.FromCompanyId = 10
This can then be translated into Linq using the LEFT OUTER JOIN approach ( How to implement left join in JOIN Extension method )
Note that if you're using Entity Framework then you don't need to worry about doing any of this at all! Just use Include:
var query = db.Inbox
.Include( i => i.User )
.Include( i => i.User.Merchandise )
.Include i => i.Merchandise )
.Where( i => i.ToCompanyId = 10 || i.FromCompanyId == 10 );
I have a join query and i want to filter the result of this query by using distinct. I want to get only one of the shoes which has same brand, model, primary color and secondary color. How can i make this ? Here is my join query.
var query = from b in db.BrandTbls.AsQueryable()
join m in db.ShoeModelTbls on b.BrandID equals m.BrandID
join s in db.ShoeTbls on m.ModelID equals s.ModelID
join i in db.ShoeImageTbls on s.ShoeID equals i.ShoeID
where s.Quantity > 0
orderby m.ModelName
select new
{
s.ShoeID,
m.ModelName,
m.Price,
b.BrandName,
i.ImagePath
};
I found the solution. This query does approximately what i want to do
var query = from b in db.BrandTbls.AsQueryable()
join m in db.ShoeModelTbls on b.BrandID equals m.BrandID
join s in db.ShoeTbls on m.ModelID equals s.ModelID
join i in db.ShoeImageTbls on s.ShoeID equals i.ShoeID
group new {b,m,s,i} by new {b.BrandName,m.ModelName,m.Price,s.ShoeID,s.PrimaryColor,s.SecondaryColor,i.ImagePath} into g
select new {g.Key.ShoeID,g.Key.BrandName,g.Key.ModelName,g.Key.ImagePath,g.Key.Price};
Remove price from the output and use Distinct() like this:
var query = (from b in db.BrandTbls.AsQueryable()
join m in db.ShoeModelTbls on b.BrandID equals m.BrandID
join s in db.ShoeTbls on m.ModelID equals s.ModelID
join i in db.ShoeImageTbls on s.ShoeID equals i.ShoeID
where s.Quantity > 0
orderby m.ModelName
select new
{
s.ShoeID,
m.ModelName,
b.BrandName,
i.ImagePath
}).Distinct();