I have a linq query that joins two tables (no relation in the actual db)
the relation is:
Companies 1 - n Phones
var miniCompanies =
(from companies in db.Companies
join phones in db.Phones on companies.Id equals phones.CompanyId
select new
{
companies.Name,
phones.Phone,
}).ToList().Distinct();
this returns something like:
----------------------------
company1 | 12345 |
----------------------------
company1 | 23456 |
----------------------------
company2 | 43242 |
----------------------------
company2 | 34234 |
----------------------------
company2 | 65442 |
----------------------------
i need to get only the fisrt in Phones table not everything
how to do that?
Edit:maybe i wasn't clear about what want sorry for that.
i ment:
----------------------------
company1 | 12345 |
----------------------------
company2 | 43242 |
----------------------------
i want the first phone for each company
You can use GroupBy:
var miniCompanies =
(from companies in db.Companies
join phones in db.Phones on companies.Id equals phones.CompanyId
select new
{
companies.Name,
phones.Phone,
}).GroupBy(c=>c.Name).Select(c=>c.FirstOrDefault()).ToArray();
1 You can try with First operator
(from companies in db.Companies
join phones in db.Phones on companies.Id equals phones.CompanyId
select new
{
phones.Phone,
}).First();
Link : http://msdn.microsoft.com/fr-fr/library/vstudio/system.linq.queryable.first.aspx
2 You can also use FirstOrDefault
Link : http://msdn.microsoft.com/fr-fr/library/vstudio/system.linq.queryable.firstordefault.aspx
3 You can also use Take(1);
Link : http://msdn.microsoft.com/fr-fr/library/vstudio/bb300906.aspx
I would use the following:
var miniCompanies =
(from companies in db.Companies
join phones in db.Phones on companies.Id equals phones.CompanyId
select new
{
companies.Name,
phones.Phone,
}).ToList().Distinct().FirstOrDefault();
The 'FirstOrDefault()' method will return the first item in the collection. If the collection contains no elements, the default object will be returned. For nullable types, this will be a 'null' object. This is a good way to prevent your application from failing due to an exception related to an empty collection.
Related
Let's say I have 3 tables - 1 header and 2 detail:
Header Table
id | label
1 | foo
2 | bar
Detail 1 Table
id | date | value
1 | 2015-01-01 | 5
Detail 2 Table
id | date | value
1 | 2015-01-01 | 7
2 | 2016-02-02 | 10
I want to make a linq query that joins all three, but does not eliminate data due to one detail table not having a record where the other one does. The result should look like:
Resulting Table
id | label | date | value1 | value2
1 | foo | 2015-01-01 | 5 | 7
2 | bar | 2016-02-02 | <null> | 10
So, a null for value1, instead of the entire row being removed.
If I were writing SQL, I could write
select
h.id,
h.label,
coalesce(d1.date, d2.date) as date,
d1.value as value1,
d2.value as value2
from
header h
left join detail1 d1
on d1.id = h.id
left join detail2 d2
on d2.id = h.id
and (
d2.date = d1.date
or d1.date is null
)
Is it possible to write this using Linq? I'm using the "on new equals new " syntax, and I cannot figure out how to preserve the detail2 record when there is no matching detail1 record.
Edit: I feel like the linked answer only answers the left join portion of my question. I know I can left join in linq, but the detail2 table is joining on to both header (not a problem) and detail1. If detail1 doesn't have a record for a date in detail2, the detail2 record will not appear in the result. Using "select new{} equals new{}" doesn't allow me to use the detail2 object before the equals, so I can't write
from
h in header.AsEnumerable()
join d1.AsEnumerable().DefaultIfEmpty()
on p.Id equals d1.Id
join d2.AsEnumerable().DefaultIfEmpty()
on new {
Id = h["Id"],
Date = d1["Date"] ?? d2["Date"], // Doesn't work, can't use d2 here.
} // d1 may not have a record, so there may not be a match
equals new {
Id = d2["Id"],
Date = d2["Date"],
}
select new {
// etc...
}
To implement a join with arbitrary conditions, you need to use another from clause with a where to handle your condition. I am not sure if used with Linq to SQL what type of SQL will be produced, you may be better off with my FullOuterJoin/LeftOuterJoin IQueryable extensions.
var ans = from h in header
join d1 in detail1 on h.id equals d1.id into hd1j
from hd1 in hd1j.DefaultIfEmpty()
from d2 in detail2 where h.id == d2.id && (hd1?.date == null || hd1.date == d2?.date)
select new { h.id, h.label, date = hd1?.date ?? d2?.date, value1 = hd1?.value, value2 = d2?.value };
For my Enumerable testing, I put in the conditional operators. You should remove them if testing against IQueryable (e.g. Linq to SQL).
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"
};
I'm developing a system using WinForms C#.
I have two relational tables:
USER_PROFILE
| ID | NAME |
1 prof1
PERMISSIONS
| UPROFILE_ID | PERMISSION |
1 abc
1 acb
1 bca
I need to show the profiles on screen.
I dont know if it's the best fitting solution for my case, or how to implement it. My idea is to display in a DataGridView like this:
| Profile | Permission1 | Permission2 | Permission3 |
prof1 abc acb bca
Any suggestions?
Thanks in advance.
You can use 'case switch' to convert row into column. In actual select query you can construct column list dynamically.
Example:-
SQL> select id,name from user_profile;
ID NAME
1 U1
2 U2
SQL> select id,pname from per order by 1;
ID PNAME
1 A
1 B
2 C
SQL> select distinct u.name user_name, case when p.pname='A'then p.pname else null end permission1, case when p.pname='B' then p.pname else null end permission2 from user_profile u, per p where u.id=p.id order by 1;
USER_NAME PERMISSION1 PERMISSION2
-------------------- -------------------- --------------------
U1 A
U1 B
U2
I have a table:
Group | BasalArea | SpeciesName
1 | 3.6 | Palustris
1 | 45.0 | MSO
2 | 4.2 | Oak
2 | 2.0 | MSO
...
From this table, I would like to get the species name with the highest basal area grouped by the Group field, which would look like this:
Group | BasalArea | SpeciesName
1 | 45.0 | MSO
2 | 4.2 | Oak
Using SQL, I can get the highest basal area:
SELECT Group, Max(BasalArea)
FROM TABLE
GROUP BY Group
I can't figure out how to also get the species name without doing some looping. Is this possible? What are the strategies for handling ties?
This is simpler in LINQ2SQL than in SQL:
var res = source.MyTable
.GroupBy(item => item.Group)
.Select(g => g.OrderByDescending(item => item.BasalArea).First())
.ToList();
This will return the list of items with largest values of BasalArea in its Group, together with SpeciesName.
In SQL you would need to join back to the original table, like this:
SELECT * FROM TABLE b
JOIN (
SELECT Group, Max(BasalArea) as BasalArea
FROM TABLE
GROUP BY Group
) t on t.Group = b.Group AND t.BasalArea = b.BasalArea
Try this:
var froup = categories.GroupBy(g => new {g.CategoryType})
.Select(g => g.OrderByDescending(i => i.CategoryID).First())
.ToArray();
What sasblinkenlight said would be the LINQ. Out of curiosity, here is a potential SQL solution.
SELECT grouped.Group, raw.SpeciesName, grouped.MaBasalArea
FROM (
SELECT Group, MAX(BasalArea) as MaxBasalArea
FROM TABLE
GROUP BY Group
) grouped
INNER JOIN TABLE raw ON grouped.MaxBasalArea = raw.BasalArea AND grouped.Group = raw.Group
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.