I am trying to convert the following query in LINQ, tried different links but no luck so far. Please help with below:
SELECT T.TASKID,
T.TITLE,
T.DESCRIPTION,
T.DEADLINE,
T.CREATEDON,
(SELECT EMAIL FROM ASPNETUSERS WHERE ID = T.CREATEDBYUSERID) AS INITIATEDBY
FROM TBL_TASKMEMBERS AS M
INNER JOIN TBL_TASKS AS T ON M.TASKID = T.TASKID
INNER JOIN ASPNETUSERS AS U ON M.USERID = U.ID
WHERE m.UserId = '95d2f49c-0ae6-4571-9d7b-1c498ad0bfac'
Thanks in advance !
Try this:
var result =
from member in TBL_TASKMEMBERS
join task in TBL_TASKS on member.TASKID equals task.TASKID
join user in ASPNETUSERS on user.ID equals member.USERID
join usermail in ASPNETUSERS on usermail.ID equals task.CREATEDBYUSERID
where member.UserId = '95d2f49c-0ae6-4571-9d7b-1c498ad0bfac'
select new { TASKID = task.TASKID, TITLE = task.TITLE, DESCRIPTION = taks.DESCRIPTION, DEADLINE = task.DEADLINE, CREATEDON = task.CREATEDON, INITIATEDBY = usermail.EMAIL };
Related
I have the following query I want to replicate in LINQ:
SELECT A.id,C.quotas, C.fundingid, C.date
,(SELECT value FROM public.fundingsharevalue WHERE fundingid = C.fundingid AND date = C.date) as sharevalue
FROM public.goal A
INNER JOIN public.goaltransactionfunding C ON A.id = C.goalid
where A.userid = 25
Code in LINQ:
var balance = from goal in _context.Goals
join goaltransactionfunding in _context.Goaltransactionfundings on goal.Id equals goaltransactionfunding.Goalid
join user in _context.Users on goal.Userid equals user.Id
where goal.Userid == userid
select new BalanceViewModel
{
FundingId = goaltransactionfunding.Fundingid,
ShareValue = (from fundingsharevalue in _context.Fundingsharevalues
where fundingsharevalue.Id == goaltransactionfunding.Fundingid && fundingsharevalue.Date.Equals(goaltransactionfunding.Date)
select fundingsharevalue.Value).FirstOrDefault(),
Quotas = (double)goaltransactionfunding.Quotas,
Fecha = goaltransactionfunding.Date,
GoalId = goal.Id
};
The problem is with Sharevalue subquery, it doesn't show values when actually it has.
It seems that the query does not fetch anything when it should fetch as it does in SQL.
I'm attaching images for both outputs.
QueryShareValues Image
LINQ sharevalues Image
Can you please let me know how to write a LINQ from for the following SQL query,
Select cr.Id
from [dbo].[User] usr, [dbo].[LikesStaging] lk, [dbo].[ChangeRequestStaging] cr
where usr.CustomerId=lk.[LikedBy] and usr.[Id] = 'user' and lk.[ChangeRequestId] = cr.[Id]
Was trying with the following query, but was not able to add usr.[Id] = 'user' condition in my linq query.
var result = from usr in lstUser
join lk in lstLikeStaging
on usr.CustomerId equals lk.LikedBy
join cr in lstChangeRequests
on lk.ChangeRequestId equals cr.Id
select new
{
cr.Id
};
Please let me know how to add this condition here.
var result = from usr in lstUser
join lk in lstLikeStaging on usr.CustomerId equals lk.LikedBy
join cr in lstChangeRequests on lk.ChangeRequestId equals cr.Id
where usr.id=="user"
select new
{
cr.Id
};
var result = from usr in lstUser
where usr.Id == "user"
join lk in lstLikeStaging
on usr.CustomerId equals lk.LikedBy
join cr in lstChangeRequests
on lk.ChangeRequestId equals cr.Id
select new
{
cr.Id
};
I've got the following LINQ Query
from s in db.tblSave
join a in db.tblAssessment on s.AssessmentID equals a.Id
join staff in db.tblStaff on s.StaffID equals staff.Id
join student in db.tblStudent on s.StudentID equals student.Id
join signed in db.tblSaveSigned on s.Id equals signed.SaveID
select new
{
SaveID = s.Id,
StaffName = staff.StaffName,
AssessmentName = a.AssessmentName,
StudentName= student.StudentName,
CreatedDate = s.CreatedDate,
SignedDate = signed.SignedDate}
tblSaveSigned may not have a record against tblSave which is excluding some tblSave records. How do I include all tblSave records in my results? It's an outer join in SQL but not sure how to do it in LINQ.
Thanks in advance.
Is there an SQL to LINQ tool?
Below is the correct LINQ Query for put FULL OUTER JOIN on tblSave for tblSaveSigned. Thanks for NetMage for pointing me in the right direction.
from s in db.tblSave
join a in db.tblAssessment on s.AssessmentID equals a.Id
join staff in db.tblStaff on s.StaffID equals staff.Id
join student in db.tblStudent on s.StudentID equals student.Id
join signed in db.tblSaveSigned on s.Id equals signed.SaveID into signedj
from signed in signedj.DefaultIfEmpty()
select new
{
SaveID = s.Id,
StaffName = staff.StaffName,
AssessmentName = a.AssessmentName,
StudentName= student.StudentName,
CreatedDate = s.CreatedDate,
SignedDate = signed.SignedDate}
Maybe a quite easy question but I'm new in Linq to SQL. I have two tables
User : UserId,name,Password,Email
USER_TABLE: Id, UserId, FirstName,LastName......
I would like a query that gives for example: userID=3 then give all the details (FirstName,LastName etc)
How can I join these two tables? I would prefer C# code if it is possible!
You do not have to do all the plumbing yourself. If you have the right foreign keys in the database you will not have to join yourself.
You can just do:
var query = from u in db.Users
select new { User = u;
FirstName = u.UserTables.FirstName }
for an inner join use something like:
var query = from u in db.Users
join ut in db.UserTables on u.UserId equals ut.UserId
select new
{
User = u,
Extra = ut
};
It's possible to join tables using linq:
E.g :
var test = (from a in DataContext.User
join b in DataContext.UserTable on a.UserId equals b.UserId
select new
{
UserId = a.UserId,
FirstName = b.FirstName
LastName = b.LastName
}).ToList();
Regards
Like this perhaps:
var joinedResult = from u in context.User
join u2 in context.UserTable on u.UserId equals u2.UserId
select new {
UserId = u.UserId,
FirstName = u2.FirstName
};
I guess your example is just an example, right? Cause it doesn't make much sense splitting that data into two tables.
I am fairly new to Linq To SQL but trying to run what should be a fairly simple SQL query and can't figure out how to make it play nice in LINQ.
SELECT Users.Id, Users.Id AS Expr1, Users.FirstName, Users.LastName,
User_x_Territory.UserID
FROM Users LEFT OUTER JOIN
User_x_Territory ON User_x_Territory.UserID = Users.Id
GROUP BY Users.Id, Users.Id, Users.FirstName, Users.LastName, User_x_Territory.UserID
HAVING (COUNT(User_x_Territory.UserID) = 0)
Just trying to get all users that do not have a territory assigned, the only way to tell if they have a territory is to check the user_x_territory gerrund.
I am able to get all of the users out of my DB with this:
var users = from u in db.Users
join uXt in db.User_x_Territories on u equals uXt.User into gerr
from users in gerr.DefaultIfEmpty()
select users;
But from there I can't figure out how to add a group by/having to refine the search results to only show users with no territories.
Thanks for any help.
I suggest the following solution.
db.Users.Where(u => u.User_x_Territories.Count == 0)
edit: heh, guess someone beat me to it :(
from t in db.Users
join t0 in db.User_x_Territory on new { UserID = t.Id } equals new { UserID = t0.UserID } into t0_join
from t0 in t0_join.DefaultIfEmpty()
group new {t, t0} by new {
t.Id,
Column1 = t.Id,
t.FirstName,
t.LastName,
t0.UserID
} into g
where g.Count() == 0
select new {
Id = g.Key.Id,
Expr1 = g.Key.Id,
g.Key.FirstName,
g.Key.LastName,
UserID = g.Key.UserID
}
I don't know how efficient this is (I'm guessing not very), but you could try something like this:
var users = (from u in db.Users
join uXt in db.User_x_Territories on u equals uXt.User into gerr
from users in gerr.DefaultIfEmpty()
select u).Where(u => u.User_x_Territories.Count == 0);