How to perform LINQ JOIN with WHERE - c#

I need help to perform JOIN using LINQ with WHERE clause.
The problem is when CaseId in events is null(not all events are case related)
it results in NOT showing event.
Here is my code:
var queryEvents = (from e in db.events
join u in db.users on e.UserID equals u.UserID
join c in db.cases on e.CaseID equals c.CaseID
where e.UserID == Program.loggedUser.UserID || (e.UserGroupID == Program.loggedUser.UserGroupID && c.AccessLvl>0)
select new { User = u.FirstName + " " + u.LastName, e.Name, e.Description, e.StartDate }).OrderByDescending(x => x.StartDate);
gvAppointments.DataSource = queryEvents.ToList();
I found some examples on how to use LINQ LEFT JOIN using INTO but then I have problem with WHERE statement.I honestly don't know where to put it.
Can someone please help me with this?

var queryEvents = (from e in db.events
join u in db.users on e.UserID equals u.UserID
join c in db.cases on e.CaseID equals c.CaseID into cases
from subC in cases.DefaultIfEmpty()
where e.UserID == Program.loggedUser.UserID || (e.UserGroupID == Program.loggedUser.UserGroupID && subC?.AccessLvl ?? 0 > 0)
select new { User = u.FirstName + " " + u.LastName, e.Name, e.Description, e.StartDate }).OrderByDescending(x => x.StartDate);
gvAppointments.DataSource = queryEvents.ToList();
You only must be aware, that subC might be now null, so you have to go for a default value, accessing it's properties.

Related

need to convert sql join query with count into linq and pass it to view

select COUNT([User].UserId)
from [User] join Team on [User].TeamId = Team.TeamId where Team.TeamId =2
Here's what I have so far, but I can't figure out how to implement
var countUser = from u in db.Users
join t in db.Teams
on u.TeamId equals t.TeamId
where (u.TeamId == 11)
select new
{
};
try this
var countUser = (from u in db.Users
join t in db.Teams
on u.TeamId equals t.TeamId
where (u.TeamId == 11)
select new
{
u.UserId
}).Count();

simplifying two queries into one with the same types linq

I have this code:
var commentData = from o in quack.BlogComments
join u in quack.AdminUsers
on o.UserId equals u.AdminUserId
where blogid == o.BlogId
select new
{
o.Comment,
o.CommentDate,
u.FirstName,
u.LastName
};
var commentData2 = from o in quack.BlogComments
join u in quack.RegularUsers
on o.UserId equals u.RegularUserId
where blogid == o.BlogId
select new
{
o.Comment,
o.CommentDate,
u.FirstName,
u.LastName
};
var l = commentData.ToList();
l.AddRange(commentData2);
As you can see above I am doing 2 different queries to the database and then adding them together to generate a single list to be used in the gridview.
What I want is to only use 1 query to the database and will result to two of those table combined.
How can I do it? is it possible with multiple joins?
You should use Concat:
var commentData = (from o in quack.BlogComments
join u in quack.AdminUsers
on o.UserId equals u.AdminUserId
where blogid == o.BlogId
select new
{
o.Comment,
o.CommentDate,
u.FirstName,
u.LastName
}).Concat(from o in quack.BlogComments
join u in quack.RegularUsers
on o.UserId equals u.RegularUserId
where blogid == o.BlogId
select new
{
o.Comment,
o.CommentDate,
u.FirstName,
u.LastName
});
var l = commentData.ToList();

Convert SqlServer Query to LINQ

I have written the below given query in SQL server 2008:
(SELECT p.postid,
p.title,
p.disabled,
l.locationname
FROM posts p
INNER JOIN categories c
ON p.categoryid = c.categoryid
INNER JOIN users u
ON p.userid = u.userid
INNER JOIN tags t
ON p.tagid = t.tagid
INNER JOIN locations l
ON p.locationid = l.locationid
LEFT JOIN postimages pm
ON p.postid = pm.postid
WHERE p.categoryid = 1
GROUP BY p.postid,
p.title,
p.disabled,
l.locationname)
ORDER BY p.postid DESC
I want to write the above query in LINQ.
I tried a little and able to write the query below:
var objPosts = (from p in _dbcontext.Posts
join us in _dbcontext.Users on p.UserId equals us.UserId
join tag in _dbcontext.Tags on p.TagId equals tag.TagId
join cat in _dbcontext.Categories on p.CategoryId equals cat.CategoryId
join loc in _dbcontext.Locations on p.LocationId equals loc.LocationId
join img in _dbcontext.PostImages on p.PostId equals img.PostId into gj
from postimg in gj.DefaultIfEmpty()
where p.Disabled == false && p.CategoryId == userPost.CategoryId || p.UserId == userPost.UserId || p.TagId == userPost.TagId || p.LocationId == userPost.LocationId
orderby p.PostId descending
select new
{
PostId = p.PostId,
PostTitle = p.Title,
//ImageInfo = postimg.ImagePath,
//ThumbNailInfo = p.ThubNailInfo,
PostShortDescription = p.ShortDescription,
UserId = us.UserId,
UserName = us.Name,
TagId = tag.TagId,
TagTitle = tag.Title,
CategoryId = cat.CategoryId,
CategoryName = cat.CategoryName,
LocationId = loc.LocationId,
LocationName = loc.LocationName
});
I am unable to apply the group by logic in LINQ. Can anyone please convert my SQL to LINQ. Thanks
group new { p, l } by new
{
p.postid,
p.title,
p.disabled,
l.locationname
} into g
orderby g.Key.PostId descending
select new
{
g.key.postid,
g.key.title,
g.key.disabled,
g.key.locationname
}
The missing part in your query is that you are not selecting anything...
You got the joins ok, left outer join ok, predicates (where), ordering, but no projection, and no group by.
Below should get you started:
group p by p.PostId into pg
select new {Post=p, Location=loc};
This would return return a collection of new objects whose properties Post and Location.

Linq To SQL - Having and Group By

I've this query below working fine. However I want to implement it using Linq.
select u.ID, u.NAME
from Task t
join BuildingUser bu ON bu.ID_BUILDING = t.ID_BUILDING
join [User] u ON u.ID = bu.ID_USER
where t.ID IN (2,9) AND u.ID != t.ID_USER
group by u.id, u.name
having count(bu.ID_BUILDING) = (SELECT COUNT(t2.ID_BUILDING) FROM Task t2 WHERE t2.ID IN (2,9))
I don't know how to Group and use Having clause at the same time.
You can try something like this:
var ids = new[] { 2, 9 };
var results =
from t in db.Tasks
join bu in db.BuildingUsers on t.ID_BUILDING equals bu.ID_BUILDING
group bu by bu.ID_BUILDING into bg
join u in db.Users on bg.Key equals u.ID
where ids.Contains(t.ID) && u.ID != t.ID_USER
group u by new { u.ID, u.NAME } into g
where bg.Count() == db.Tasks.Count(t2 => ids.Contains(t2.ID))
select g.Key;
Or if you have navigation properties set up correctly, you can try this:
var ids = new[] { 2, 9 };
var results =
from t in db.Tasks.Where(x => ids.Contains(x.ID))
from u in t.BuildingUsers.SelectMany(bu => bu.Users)
.Where(x => x.ID != t.ID_USER)
group u by new { u.ID, u.NAME } into g
where t.BuildingUsers.Count() == db.Tasks.Count(x => ids.Contains(x.ID))
select g.Key;

Linq to entities query with nested query on the same table

I have a SQL query:
SELECT distinct A.stock_value_site,
A.BALANCE_QUANTITY,
A.BALANCE_NOMINAL_VALUE,
A.BALANCE_INDEXED_VALUE,
V.STOCK_VALUE_ORDER,
U.BALANCE_QUANTITY
FROM svr A,
svt V,
svu U
WHERE V.Code = 500 and
A.Id = U.ID (+) and
A.Id = (SELECT max(B.id)
FROM svr B,
sts
WHERE A.stock_value_site = B.stock_value_site and
B.Id = sts.ID(+) and
B.item = _item and
B.date < _from_date and
B.Id IS NULL)
I tried to convert this query to Linq to entities:
var data = (from A in _context.svr
join V in _context.svt on A.svtId equals V.Code
join U in _context.svu on A.Id equals U.Id into groupA
from gA in groupA.DefaultIfEmpty()
where V.Code == _openBalanceRecordId &&
A.Id == (from B in _context.svr
join st in _context.sts on B.Id equals st.Id into groupB
from gB in groupB.DefaultIfEmpty()
where A.svsId == B.svsId &&
B.ItemId == _item &&
B.Date < _startDate.Date
select B.Id).Max()
select new
{
A.stock_value_site,
A.BALANCE_QUANTITY,
A.BALANCE_NOMINAL_VALUE,
A.BALANCE_INDEXED_VALUE,
V.STOCK_VALUE_ORDER,
gA == null ? 0 : gA.BALANCE_QUANTITY
}).Distinct().ToList();
When I run the SQL query in oracle I get 33 records back. But when I run it using Linq to entities I don't get any record.
What am I doing wrong?
I believe there are still some issues with you're original query, if I understand what you're trying to do, this should work better:
var results =
(from a in _context.svr
from v in _context.svt.Where(t => t.Code == 500)
join u in _context.svu on u.Id equals a.Id into gU
from x in gU.DefaultIfEmpty()
where a.Id ==
(from b in _context.svr
join s in _context.sts on b.Id equals s.Id
where b.item = _item
and b.date < _from_date
and b.svsId == a.svsId
select b.Id).Max()
select new
{
a.stock_value_site,
a.BALANCE_QUANTITY,
a.BALANCE_NOMINAL_VALUE,
a.BALANCE_INDEXED_VALUE,
v.STOCK_VALUE_ORDER,
x == null ? 0 : x.BALANCE_QUANTITY
}).Distinct().ToList();
Or possibly this:
var results =
(from a in _context.svr
from v in _context.svt.Where(t => t.Code == 500)
join u in _context.svu on u.Id equals a.Id into gU
from x in gU.DefaultIfEmpty()
let subQuery =
(from b in _context.svr
join s in _context.sts on b.Id equals s.Id
where b.item = _item
and b.date < _from_date
select new { b.Id, b.svsId })
join y in subQuery on a.svsId equals y.svsId into gY
where a.Id = y.Max(b => b.Id)
select new
{
a.stock_value_site,
a.BALANCE_QUANTITY,
a.BALANCE_NOMINAL_VALUE,
a.BALANCE_INDEXED_VALUE,
v.STOCK_VALUE_ORDER,
x == null ? 0 : x.BALANCE_QUANTITY
}).Distinct().ToList();

Categories