How to Group By Distinct in EF - linq

I need to translate this SQL to Linq for use with Entity Framework:
SELECT TheDate, COUNT(DISTINCT IPAddress) as Count
FROM TheTable Group By TheDate
Order By TheDate
The difficulty is the Count(Distinct IPAddress).
Greg

from x in db.TheTable
group x by x.TheDate into g
orderby g.Key
select new
{
TheDate = g.Key,
DistinctIPCount = g.Select(x => x.IPAddress).Distinct().Count()
}

Related

How do I convert this query into LINQ?

SELECT *
FROM
ProcedureLookup ProcLookup
INNER JOIN (SELECT PatientProcedures.PP_ProcedureId, COUNT(*) as ProcCount
FROM (PatientProcedures INNER JOIN Treatments ON PatientProcedures.PP_TreatmentId = Treatments.TS_TreatmentId)
WHERE YEAR(Treatments.TS_Date) = YEAR(GETDATE())
GROUP BY PatientProcedures.PP_ProcedureId) cyearProc ON ProcLookup.PL_ProcedureId = cyearProc.PP_ProcedureId
ORDER BY ProcCount DESC;
Here ProcedureLookup, Treatments, PatientProcedures are the tables.
Here linq query.
var result = (from ProcLookup in db.ProcedureLookup
join cyearProc in (
from p in db.PatientProcedures
join t in db.Treatments on p.PP_TreatmentId equals
t.TS_TreatmentId
where
t.TS_Date.Year == DateTime.Now.Year
group p by p.PP_ProcedureId into g
select new
{
PP_ProcedureId = g.Key,
ProcCount = g.Count()
}
) on ProcLookup.PL_ProcedureId equals
cyearProc.PP_ProcedureId
orderby cyearProc.ProcCount descending
select new
{
// Columns
PP_ProcedureId = ProcLookup.PP_ProcedureId,
ProcCount = cyearProc.ProcCount
}).ToList();

how to write this statement in linq to sql

SELECT TOP (5)
Sales.Product, Sales.Product_Price, COUNT(*) AS CNT,
Products.Category, Products.IMG_URL, Products.Rate_Avg
FROM
Sales
INNER JOIN
Products ON Sales.Product = Products.Product
GROUP BY
Sales.Product, Sales.Product_Price,
Products.Category, Products.IMG_URL, Products.Rate_Avg
HAVING
(COUNT(*) > 1)
ORDER BY CNT DESC
Most of that query has a 1-to-1 correspondence to the equivalent linq-to-sql expression. Though the TOP (5) part needs to be added to the end.
(from s in db.Sales
join p in db.Products on s.Product equals p.Product
group s by new { s.Product, s.Product_Price, p.Category, p.IMG_URL, p.Rate_Avg } into g
where g.Count() > 1
orderby g.Count() descending
select new
{
g.Key.Product,
g.Key.Product_Price,
CNT = g.Count(),
g.Key.Category,
g.Key.IMG_URL,
g.Key.Rate_Avg,
}).Take(5)

How to write the query using linq?

How to write the query using linq?
SELECT
(SELECT SUM([quantity] * [price_usd])
FROM [vi].[dbo].[SALES]
WHERE [type_id] =1 and [date_id] in(200701,200702,200703,200704,200705,200706,200707,200708,200709,200710,200711,200712)) as y2007,
(SELECT SUM([quantity] * [price_usd])
FROM [vi].[dbo].[SALES]
WHERE [type_id] =1 and [date_id] in(200801,200802,200803,200804,200805,200806,200807,200808,200809,200810,200811,200812)) as y2008,
(SELECT SUM([quantity] * [price_usd])
FROM [vi].[dbo].[SALES]
WHERE [type_id] =1 and [date_id] in(200901,200902,200903,200904,200905,200906,200907,200908,200909,200910,200911,200912)) as y2009
What about that kind of query:
From s in Sales
where s.type_id == 1
group s by s.date_id.ToString().Substring(0,4) into g
select New { Year = g.Key, Sum = g.Sum(i => i.quantity * i.price_usd) }
I have no opportunity to try it on iPad, but the idea is clear and it should work.

How to use GroupBy properly in LINQ?

I have 4 tables: Post, Category, Relation and Meta
A category can contains multiple posts, and the relation between them is stored in Relation table. A post then can has many extra info that are stored in Meta table. I want to list all post with categories and extra infos, then group them by post's ID.
I have the following query
select p.ID, p.Title, t.Name, m.Key, m.Value from Post p
left join Relation r on p.ID = r.Child
left join Category c on r.Parent = c.ID
left join Meta m on p.ID = m.Object
where m.Type = 'news'
order by p.ID
and with these sample data:
Post
ID Title
1 A
Category
ID Name
1 Tips
2 Tricks
Meta
ID Object Key Value
1 1 Key1 Value 1
2 1 Key2 Value 2
Relation
ID Child Parent
1 1 1
2 1 2
then the result will be
PostID Title Category Key Value
1 A Tips Key1 Value1
1 A Tips Key2 Value2
1 A Tricks Key1 Value1
1 A Tricks Key2 Value2
and I expected the result to be
PostID Title Categories Meta
1 A Tips, Tricks Key1=Value1, Key2=Value2
I wonder if we can convert the query from SQL to LINQ to Entities with EF v4 and the result is stored in a class like this
class Result
{
long ID,
string Title,
List<string> Categories,
Dictionary<string, string> Meta
}
Any helps would be appreciated.
What's the final result you expect from the query
I personally prefer to write the query like
var q = from r in Relation
join p in Post on r.Child equals p.ID
join t in Term on r.Parent equals t.ID
let x = new { p.ID, p.Title, t.Name }
group x by x.ID into g
select g;
this way I think (not sure) the sql generated will be simpler
Now that you're wanting to use EntityFramework, you would merely need to set up you database, edmx with a Result table with an ID and a Title, then Category and Meta tables. Then add one-to-many relationships from the Result table to each the Category and Meta tables.
I'm not 100% sure what you're trying to do, but obviously if you're grouping, the results have to be grouped by anything in the resultset, or be aggregated data. This query will retrieve your results and group by PostId, PostTitle, and CategoryName, generating a single SQL Statement:
var query = from p in Posts
from r in Relations
.Where(r => p.ID == r.Child)
.DefaultIfEmpty()
from c in Categories
.Where(c => r.Parent == c.ID)
.DefaultIfEmpty()
group p by new {ID = p.ID, Title = p.Title, Name = c.Name} into z
select new { ID = z.Key.ID, Title = z.Key.Title, Name = z.Key.Name };
Here is the SQL Generated by this statement:
SELECT [t3].[ID], [t3].[Title], [t3].[value] AS [Name]
FROM (
SELECT [t0].[ID], [t0].[Title], [t2].[Name] AS [value]
FROM [Post] AS [t0]
LEFT OUTER JOIN [Relation] AS [t1] ON [t0].[ID] = [t1].[Child]
LEFT OUTER JOIN [Category] AS [t2] ON [t1].[Parent] = [t2].[ID]
) AS [t3]
GROUP BY [t3].[ID], [t3].[Title], [t3].[value]
Here is the SQL Generated by your original statement:
SELECT [t0].[ID] AS [Key]
FROM [Post] AS [t0]
INNER JOIN [Relation] AS [t1] ON [t0].[ID] = [t1].[Child]
INNER JOIN [Category] AS [t2] ON [t1].[Parent] = [t2].[ID]
GROUP BY [t0].[ID]
GO
-- Region Parameters
DECLARE #x1 Int SET #x1 = 1
-- EndRegion
SELECT [t0].[ID], [t0].[Title], [t2].[Name]
FROM [Post] AS [t0]
INNER JOIN [Relation] AS [t1] ON [t0].[ID] = [t1].[Child]
INNER JOIN [Category] AS [t2] ON [t1].[Parent] = [t2].[ID]
WHERE ((#x1 IS NULL) AND ([t0].[ID] IS NULL)) OR ((#x1 IS NOT NULL) AND ([t0].[ID] IS NOT NULL) AND (#x1 = [t0].[ID]))
GO
-- Region Parameters
DECLARE #x1 Int SET #x1 = 2
-- EndRegion
SELECT [t0].[ID], [t0].[Title], [t2].[Name]
FROM [Post] AS [t0]
INNER JOIN [Relation] AS [t1] ON [t0].[ID] = [t1].[Child]
INNER JOIN [Category] AS [t2] ON [t1].[Parent] = [t2].[ID]
WHERE ((#x1 IS NULL) AND ([t0].[ID] IS NULL)) OR ((#x1 IS NOT NULL) AND ([t0].[ID] IS NOT NULL) AND (#x1 = [t0].[ID]))

Linq with group by having count

how do I write this query in linq (vb.net)?
select B.Name
from Company B
group by B.Name
having COUNT(1) > 1
Like this:
from c in db.Company
group c by c.Name into grp
where grp.Count() > 1
select grp.Key
Or, using the method syntax:
Company
.GroupBy(c => c.Name)
.Where(grp => grp.Count() > 1)
.Select(grp => grp.Key);
For anyone looking to do this in vb (as I was and couldn't find anything)
From c In db.Company
Select c.Name Group By Name Into Group
Where Group.Count > 1
Below solution may help you.
var unmanagedDownloadcountwithfilter = from count in unmanagedDownloadCount.Where(d =>d.downloaddate >= startDate && d.downloaddate <= endDate)
group count by count.unmanagedassetregistryid into grouped
where grouped.Count() > request.Download
select new
{
UnmanagedAssetRegistryID = grouped.Key,
Count = grouped.Count()
};

Resources