This question already has answers here:
LINQ Left Join And Right Join
(4 answers)
Closed 9 years ago.
I'm trying to convert this query (Already working)
select [User].ID AS ID_USER
from [User]
right join Building on Building.ID = 4 AND
Building.ID_USER_RESPONSIBLE <> [User].ID
where [User].ID_MANAGER = 1
To Linq to SQL, but I don't know what I'm doing wrong. Look at my trying
IList<User> lstUser = (from building in db.GetTable<Building>()
join user in db.GetTable<User>()
on new
{
ID_BUILDING = building.ID,
ID_USER = building.ID_USER_RESPONSIBLE
}
equals new
{
ID_BUILDING = 4,
ID_USER != user.ID
} into grpUser
from grp in grpUser.DefaultIfEmpty()
where building.ID_MANAGER = 1
select new
{
ID_USER =
});
I need to return all Users plus the Users which aren't responsible to any Building;
EDIT
Any of solution posted here worked.
I've resolved just doing this:
var lstUser = (from building in db.GetTable<Building>()
join user in db.GetTable<User>()
on building.ID equals 4
where user.ID_MANAGER == 1 &&
building.ID_USER_RESPONSIBLE != user.ID
select new
{
ID_USER = user.ID,
NAME = user.NAME
}).ToList();
See if the following works:
IList<User> lstUser = (from building in db.GetTable<Building>()
join user in db.GetTable<User>()
on new
{
ID_BUILDING = building.ID,
ID_USER = building.ID_USER_RESPONSIBLE,
building.ID_MANAGER
}
equals new
{
ID_BUILDING = 4,
ID_USER = user.ID,
1
} into grpUser
from grp in grpUser.DefaultIfEmpty()
select new
{
ID_USER = grp.ID
});
That being said, there seems to be a gap in your requirement, "I need to return all Users plus the Users which aren't responsible to any Building;" I may be mis-reading your statement but isn't the list of users that aren't responsible to a building a subset of the larger "All Users" set. As a result, you would always just get All Users regardless of their building membership.
Related
I'm currently having a rough time converting my SQL query to LINQ for a school project I'm using WPF and Entity Framework
here is my SQL query (working exactly as I expect)
select IngrediantName,sum(IngrediantQuantity) as Quantity, IngrediantMeasurementUnit from Users
join Shopping_List
on Users.UserID = Shopping_List.ShoppingListID
join List_Item
on List_Item.ShoppingListID = Shopping_List.ShoppingListID
join Ingrediant
on Ingrediant.IngrediantID = List_Item.IngrediantID
where Users.UserID = 1
group by IngrediantName,IngrediantMeasurementUnit
Here is the query that I have so far
var query = from user in dbContext.Users
join shoppingList in dbContext.ShoppingLists on user.UserId equals shoppingList.UserId
join listItem in dbContext.ListItems on shoppingList.ShoppingListId equals listItem.ShoppingListId
join ingrediant in dbContext.Ingrediants on listItem.IngrediantId equals ingrediant.IngrediantId
where currentUserNumber == user.UserId
select new
{
name = ingrediant.IngrediantName,
quantity = ingrediant.IngrediantQuantity,
unit = ingrediant.IngrediantMeasurementUnit,
};
Here is what i try so far
var query = from user in dbContext.Users
join shoppingList in dbContext.ShoppingLists on user.UserId equals shoppingList.UserId
join listItem in dbContext.ListItems on shoppingList.ShoppingListId equals listItem.ShoppingListId
join ingrediant in dbContext.Ingrediants on listItem.IngrediantId equals ingrediant.IngrediantId
where currentUserNumber == user.UserId
group ingrediant by ingrediant.IngrediantQuantity into x
select new
{
name = x.GroupBy(x => x.IngrediantName),
quantity = x.Sum(x => x.IngrediantQuantity),
unit = x.GroupBy(x => x.IngrediantMeasurementUnit),
};
this one return the following error wiches doesn't tell much
Argument type 'System.Linq.IQueryable1[System.Linq.IGrouping2[System.String,Microsoft.EntityFrameworkCore.Query.TransparentIdentifierFactory+TransparentIdentifier2[Microsoft.EntityFrameworkCore.Query.TransparentIdentifierFactory+TransparentIdentifier2
If someone could point me in the right direction I would appreciate it, if you need more info I will provide it for sure
Thanks
UPDATE
I got this working here the answers to the question for anyone else
var query =
from user in dbContext.Users
join shoppingList in dbContext.ShoppingLists
on user.UserId equals shoppingList.UserId
join listItem in dbContext.ListItems
on shoppingList.ShoppingListId equals listItem.ShoppingListId
join ingrediant in dbContext.Ingrediants
on listItem.IngrediantId equals ingrediant.IngrediantId
where currentUserNumber == user.UserId
group ingrediant by new { name = ingrediant.IngrediantName, unit = ingrediant.IngrediantMeasurementUnit } into x
select new
{
name = x.Key.name,
quantity = x.Sum(x => x.IngrediantQuantity),
unit = x.Key.unit,
};
if you look at the group line
group ingrediant by new { name = ingrediant.IngrediantName, unit = ingrediant.IngrediantMeasurementUnit } into x
from my understanding you use the new to create a new selector then you can use x.key.name and x.key.unit where name and unit are simply some variable
How to fix this query to meet the new .NET Core 3 update?
var rtn =
from fav in _context.AdvertFavorite
join advert in _context.Adverts on fav.AdvertID equals advert.AdvertID
join blob in _context.Blobs on advert.AdvertID equals blob.AdvertID
join user in _userManager.Users on advert.UserID equals user.Id
where fav.UserID == usrid
group new { blob, user, advert } by new { advert.AdvertID } into grp
select grp;
var rtn1 = rtn.ToList();
return rtn1;
I have 3 tables
- ERPEntry
- ERPEntryType
- ERPApp
I am trying to get data from these 3 tables using the below query but i got the error :
specified linq expression contains references to queries that are
associated with different contexts
var erpEntryInfo = (from s in ERPDB.ERPEntrys
JOIN t in ERPDB.ERPEntryTypes
on s.EntryTypeID equals t.EntryTypeID
join a in APPDB.ERPApps
on s.AppId equals a.AppId
where s.UserIDAdded == '250176'
select new ERPInfo
{
EntryId = s.EntryID,
EntryType = t.EntryTypeName,
ERPApp = a.ApplicationName,
DateAdded = s.DateAdded
}).OrderByDescending(d => d.DateAdded).Take(10).ToList();
I searched based on the errror and tried to split the above query into 2 as below.
var res = (from s in ERPDB.ERPEntrys
join t in ERPDB.ERPEntryTypes
on s.EntryTypeID equals t.EntryTypeID
where s.UserIDAdded == '250176'
select new {s.EntryTypeID, s.DateAdded, t.EntryTypeName, s.AppID }).OrderByDescending(d => d.DateAdded).Take(10).ToArray();
var y = (from a in APPDB.ERPApps
join b in res on a.AppId equals //??//
select new ERPInfo
{
EntryId = b.EntryID,
EntryType = b.EntryTypeName,
ERPApp = a.ApplicationName,
DateAdded = b.DateAdded
}).ToList();
I am having an issue in the above query to access AppId which i got into the result res..i commented with //??// in the above code
can i get any help on this.
I am relatively new to entity framework and I've been trying to write a Linq statement with Lambda that includes a simple join. I have three tables: Staff - StaffRole - Role.
I want a staff member in a certain role that satisfies a certain condition. Its very simple to write this in regular SQL:
SELECT *
FROM Staff s
INNER JOIN StaffRole sr ON s.StaffId = sr.StaffId
INNER JOIN Role r ON sr.RoleId = r.RoleId
WHERE r.Description = 'SpecialisedHealthManager'
AND s.PrimaryShm = 0
Now, writing it in a Linq statement has not given me much luck. I'm thinking it would be something like this:
var actingShm = db.Staff.Join(db.StaffRole,
inner => inner.StaffId,
outer => outer.Role,
(outer, inner) => new
{
StaffId = inner.StaffId,
FirstName = inner.Staff.FirstName,
Surname = inner.Staff.Surname,
SamAccountName = inner.Staff.SamAccountName,
RoleId = outer.Description
});
Needless to say, this is not working..
Try using it this way:
var list = from s in Staff
join sr in StaffRole on s.StaffId equals sr.StaffId
join r in Role on sr.RoleId equals r.RoleId
where r.Description == 'SpecialisedHealthManager' && s.PrimaryShm == 0
select new
{
StaffId = s.StaffId,
FirstName = s.Staff.FirstName,
Surname = s.Staff.Surname,
SamAccountName = s.Staff.SamAccountName,
RoleId = r.Description
});
Look here if you realy want to do this with method syntax LINQ:
SO Multiple tables join with lambdas
Also look here:
http://msdn.microsoft.com/pl-pl/library/bb534675(v=vs.110).aspx
for Join extension method syntax. Usage presented in your code is wrong.
You should have your associations setup so you can do this...
var actingShm = from s in db.Staff
from r in s.Roles
where r.Description == "SpecialisedHealthManager"
select new
{
StaffId = s.StaffId,
FirstName = s.FirstName,
Surname = s.Surname,
SamAccountName = s.SamAccountName,
RoleId = r.Description
});
Are you using Entity Framework or Linq2SQL?
This question already has answers here:
LINQ Join with Multiple Conditions in On Clause
(3 answers)
Closed 8 years ago.
I have 2 Tables AtdDailyAttendance and AcdAdmissionSessionDetails. I want to join these 2 tables from linq based on 2 ids, which means in Sql this join looks like
SELECT a.Id, a.DateId,
a.StudentLedgerId, a.AttendanceTypeId,
a.SchoolId, a.UserId, a.SessionId,
d.ClassId,d.MediumId,
d.StreamId, d.ShiftId,
d.SectionId
FROM AtdDailyAttendance a INNER JOIN AcdAdmissionSessionDetail d
ON a.StudentLedgerId = d.StudentLedgerId AND a.SessionId = d.SessionId
But in LINQ I'm unable to do this. I tried this way
var query =
from a in dbCOntext.AtdDailyAttendances
join b in dbCOntext.AcdAdmissionSessionDetails
on a.StudentLedgerId equals b.StudentLedgerId
// on a.SessionId equlas b.SessionId
select new
{
a.AtdSetedDatesForAttendance,
a.DateId,
a.StudentLedgerId,
a.SchoolId,
a.UserId,
a.SessionId,
b.ClassId,
b.SectionId,
b.MediumId,
b.StreamId
}
var liResult = query.ToList();
Here I'm unable to perform join between SessionId.
I think you're looking for:
var liResult = (from a in dbCOntext.AtdDailyAttendances
join b in dbCOntext.AcdAdmissionSessionDetails
on new { a.StudentLedgerId, a.SessionId }
equals new { b.StudentLedgerId, b.SessionId}
See if this will work:
dbContext.AtdDailyAttendances.Join(dbContext.AcAdmissionSessionDetails, m => m.StudentLedgerId, n => n.StudentLedgerId, (m, n) => new{
m.AcdAdmissionSessionDetails,
m.DateId,
m.StudentLedgerId,
m.SchoolId,
m.UserId,
AttendanceSessionId = n.SessionId,
AdmissionSessionId = m.SessionId,
n.ClassId,
n.SectionId,
n.MediumId,
n.StreamId
}).Where(n => n.AttendanceSessionId == n.AdmissionSessionId).ToList();