Linq Left Outer Join With newest datetime in right side - c#

I have "Orders" table where its primary key is "OrderId ":
OrderId | OrderName
------- | ----------
1 | Order X
2 | Order Y
3 | Order Z
and "OrderDetails" table where its primary key is "OrderDetailsId " foreign key is 'OrderId":
OrderDetailsId | OrderId | ItemId | DeliveryDate
-------------- | ------- | ------ | ------------
10 | 1 | AA | 1/1/2010
20 | 1 | BB | 1/1/2013
30 | 2 | CC | 1/1/2012
40 | 2 | CC | 1/1/2014
Each order has ZERO or more order details, each order detail has specific delivery date.
We want to get all the orders, whether they have order details or not, and mark just one order as VIP if it has the order detail that has the maximum "delivery date"
This is the expected output:
OrderId | OrderName | IsVIP
------- | --------- | -----
1 | Order X | NO
2 | Order Y | YES
3 | Order Z | NO (since it has no order details)
That's because the maximum delivery date is for OrderDetailsId = 40 which belongs to OrderId = 2
How to accomplish this using the most readable LINQ code

I am not sure if you have OrderDetails property in orders collection (if so then #juharr's answer is correct). But, if they are not then you can make use of group join like this:-
var result = from o in orders
join od in orderDetails
on o.OrderId equals od.OrderId into g
select new {
OrderId = o.OrderId,
OrderName = o.OrderName,
IsVIP = g.Any(x => x.DeliveryDate == orderDetails.Max(z => z.DeliveryDate))
? "Yes" : "No"
};
Here is an example Fiddle with linq-to-objects.

Use navigation properties. Note this will set IsVIP to "YES" for all orders that contain an order detail with the max delivery date.
var query = from order in db.Orders
select new
{
order.OrderId,
order.Name,
IsVIP = order.OrderDetails.Any(
od => od.DeliveryDate == db.OrderDetails.Max(x => x.DeliveryDate))
? "YES"
: "NO"
};

Related

Linq Account Hierarchy Drill Down

I have two tables, one an invoice table and the other a hierarchy table for the accounts on the invoice
Invoice Table:
| InvoiceNo| AccNo| InvoiceAmount |
--------------------------------------
| A1234| 345| 100.00 |
| A1235| 346| 95.00 |
| A1236| 347| 15.50 |
| A1237| 348| 20.10 |
Hierarchy Table
| AccNo| HierAccNo| Level|
--------------------------------------
| 123| | 1 |
| 789| 123| 2 |
| 890| 123| 2 |
| 345| 789| 3 |
| 346| 789| 3 |
| 347| 890| 3 |
| 348| 890| 3 |
What I'm trying to do is to roll up the amounts from the invoice table to the highest level AccNo which is Level1 and then on a seperate instance from the highest account number roll back down to the next levels.
So far I am able to roll up to the highest band number by the following :
var BandL2 = from invoice in db.Invoices//Roll up to level 2
join ban in db.HierarchyTable
on invoice.AccNo equals ban.Ban
where invoice.GlobalInvoiceID == globalInvoice.Id
group invoice by ban.HierAccNo into bandHierarchy
select new
{
Level2Band = bandHierarchy.Key,
Amount = bandHierarchy.Sum(m=> m.InvoiceAmount)
};
var bandHierarchyTable = db.HierarchyTable.AsQueryable();
var BandL1 = from band2 in BandL2 // Roll Up to level 1
join band1 in bandHierarchyTable
on band2.Level2Band equals band1.Ban
group band2 by band1.HierAccNo into bandL1
select new
{
Level1Band = bandL1.Key,
Amount = bandL1.Sum(m => m.Amount)
};
But now I'm having an issue reversing the process and drilling down from Level 1 as the only details from the form is the AccNo of Level 1(eg. 123).
I'm trying to do this on the fly using pop up modals as I'm drilling.
How do I drill down again so that I can get level by level amounts?
Example:
Ouput Table from Above Code
| AccNo| Amount|
--------------------------------------
| 123| 230.60 |
Then
| AccNo| Amount|
--------------------------------------
| 789| 195 |
| 890| 35.60|
And then clicking on one of the AccNo.
| AccNo| Amount|
--------------------------------------
| 345| 100|
| 346| 95 |
Thanks!
Looks like only leaf accounts can have invoices attached to it, so get the account number, check if it is leaf account, if it is leaf it should be something like that;
var hierAccNo = 123;
var details = from invoice in db.Invoices
join ban in db.HierarchyTable
on invoice.AccNo equals ban.Ban
where invoice.GlobalInvoiceID == globalInvoice.Id
and ban.HierAccNo = HierAccNo;
if it is not leaf you want your original query on level 1 I guess to get the subtotals.
var BandL2 = from invoice in db.Invoices//Roll up to level 2
join ban in db.HierarchyTable
on invoice.AccNo equals ban.Ban
where invoice.GlobalInvoiceID == globalInvoice.Id and ban.HierAccNo = hierAccNo // dont skip this
group invoice by ban.HierAccNo into bandHierarchy
select new
{
Level2Band = bandHierarchy.Key,
Amount = bandHierarchy.Sum(m=> m.InvoiceAmount)
};

Select values from one table based on specific value of another table Linq

I have 2 tables:
Location
id | user_id | latitude | longitude|
1 | 2 | 11.32323 | 11.32323 |
2 | 3 | 12.32323 | 12.32323 |
3 | 4 | 21.32323 | 12.32323 |
Task
id | user_id | status |
1 | 2 | 0 |
2 | 2 | 1 |
3 | 2 | 0 |
4 | 2 | 2 |
5 | 2 | 1 |
6 | 2 | 0 |
7 | 3 | 1 |
8 | 3 | 1 |
9 | 3 | 1 |
I want to select all rows from location table in which users have
either no record in Tasks table (for e.g. user_id = 4)
or if records
exists then all of them must have status equals to 1 (for e.g. user_id
= 3).
In above example, user_id = 2 should not be selected because it has rows in Tasks table with status other than 1.
I am not very much familiar with SQL and LINQ so any help would be appreciated.
This is the expected result:
Result
id | user_id | latitude | longitude|
2 | 3 | 12.32323 | 12.32323 |
3 | 4 | 21.32323 | 12.32323 |
Location with user_id = 2 was ignored because it has some rows in Tasks table with status other than 1.
Location with user_id = 3 was selected because all rows in Tasks table has status = 1.
Location with user_id = 4 was selected because there were no rows in Tasks table with user_id = 4.
Looking at you requirements could be this
select * from location
where user_id not in (select distinct user_id from task )
or user_id not in (select distinct user_id from task where status != 1);
Your conditions are equivalent to saying that no non-"1" value exists in task. I would write this as:
select l.*
from location l
where not exists (select 1 from tasks where t.user_id = l.user_id and t.status = 1);
I prefer not exists to not in because not in will filter out all rows if user_id is ever NULL in tasks.
Using a LEFT JOIN without a sub-SELECT:
SELECT
l.id,
l.user_id,
l.latitude,
l.longitude
FROM
Location l
LEFT JOIN Task t
ON l.user_id = t.user_id
WHERE
t.id IS NULL /* No record in tasks table */
OR (t.id IS NOT NULL AND l.status = 1) /* if records exists then all of them must have status equals to 1 */

LINQ join tables with value if no match

I know that there are some examples but I could not apply them on my code. I am pretty new to Linq and SQL. I have two tables I want to join.
First Table:
--------------
| Id | Count |
--------------
| 1 | 10 |
--------------
| 2 | 4 |
--------------
Second Table:
--------------
| Id | Name |
--------------
| 1 | Tom |
--------------
| 2 | John |
--------------
| 3 | Nick |
--------------
| 4 | Max |
--------------
As you can see, the second table has more records than the first. My goal is to join them based on the Id. The problem is that after I have joined the tables it only displays the matching records, which is Id 1 and 2. Though I want to display every Id (from 1 to 4) and if there is no match in both tables, there should be a default value 0.
It should look like this:
----------------------
| Id | Name | Count |
----------------------
| 1 | Tom | 10 |
----------------------
| 2 | John | 4 |
----------------------
| 3 | Nick | 0 |
----------------------
| 4 | Max | 0 |
----------------------
So far I have got this code:
// first table
var listCount = entity.tblKundes.Where(x => x.Studio == 2)
.Select(x => new { x.Id, x.Name})
.GroupBy(x => x.Name).ToList();
// second table
var listBerater = entity.tblUsers.Where(x => x.Studio == 2)
.Select(x => new { x.Id, x.Name})
.ToList();
// This join should be edited so that it displays non matching records as well
var test = listCount.Join(
listBerater,
count => count.Key,
berater => berater.Id,
(count, berater) => new { listCount = count, listBerater = berater }
).ToList();
Edit:
var test2 = (from list in listCount
join berater in listBerater on list.Berater equals berater.Id into gj
from sublist in gj.DefaultIfEmpty()
select new { sublist.Id, sublist.Nachname, sublist.Vorname }).ToList();
There is a typical concept in every Structured Query Languages which is called "Left join". Left-Join means that you will have all rows of data from first table even there is no equivalent in the second one. "Inner-Join" is a little different and only looks for matched rows of data.
Here you can find enough and complete information about your issue.
Left Join

How to Join with a column having Comma Separated Values in Linq

I have one master table Category.
ID | Name
----------
1 | Category1
2 | Category2
3 | Category3
4 | Category4
And Another Table Details have field like
ID | CategoryId | Detail
--------------------
1 | 1,2,3 | Test1
2 | 3,4 | Test2
Here the Category Id stored as comma separated values.
Now i want the result as
ID | CategoryName
----------------
1 | Category1,Category2,Category3
2 | Category3,Category4
AnyOne Have idea ..??
You can use link this:
private static void commaSeperate(List<classname> obj)
{
string delimeter = ",";
Console.WriteLine(obj.Aggregate((i, j) => new classname { Name = (i.Name + delimeter + j.Name) }).Name);
Console.ReadKey();
}
This is just a sample, please modify according to your conditions.
Hope this will help you.
This could be the solution if our retrieve the data into memory first.
var q = from d in Details
from m in Master
select new {Id = d.Id, CategoryName = String.Join(m.Where(i=> d.CategoryId.Split(',').Cast<int32>().Contains(i.Id).Select(i => i.Name).ToArray(), ',')}
You can't join these two tables but I think below query would work for your result:
(from de in datacontextobj.Details
from ca in datacontextobj.Category
where de.CategoryId.Contains(ca.ID)
select de.ID, ca.Name).ToList();

Get distinct columns with order by date

I have these following two tables:
Job Title | PostDate | CompanyId
Assitant | 12/15/10 | 10
Manager | 12/1/10 | 11
Developer | 12/31/10 | 10
Assitant | 12/1/10 | 13
PM | 11/29/10 | 12
CompanyId | Name
10 | Google
11 | Yahoo
12 | Microsoft
13 | Oracle
Now i would like to get 3 different companies with the jobs sorted by post date. The result table would be following:
Job Title | PostDate | CompanyName
Developer | 12/31/10 | Google
Manager | 12/1/10 | Yahoo
Assitant | 12/1/10 | Oracle
How can I achieve that using a linq query? Any help will be appreciated...
I think that would be something like:
var query = from company in db.Companies
join job in db.Jobs on company.CompanyId equals job.CompanyId
group job by company into jobsByCompany
let lastJob = jobsByCompany.OrderByDescending(x => x.PostDate)
.First()
orderby lastJob.PostDate descending
select new
{
JobTitle = lastJob.JobTitle,
PostDate = lastJob.PostDate,
CompanyName = jobsByCompany.Key.Name
};
It's a little odd to do a join and then a group - we could do a GroupJoin instead, but then discard empty options:
var query = from company in db.Companies
join job in db.Jobs on company.CompanyId equals job.CompanyId
into jobsByCompany // Make this a group join
let lastJob = jobsByCompany.OrderByDescending(x => x.PostDate)
.FirstOrDefault()
where lastJob != null
orderby lastJob.PostDate descending
select new
{
JobTitle = lastJob.JobTitle,
PostDate = lastJob.PostDate,
CompanyName = company.Name
};
EDIT: Note that doesn't just take the top three results. Use query = query.Take(3); to just get the first three results.

Categories