select records with max aggregate using Linq To SQL - c#

I have following two tables:
DocumentType
Id INT,
Name VARCHAR(100),
Active BIT,
CreatedBy INT
Document
Id INT,
DocumentTypeId INT,
Version SMALLINT,
Text NTEXT
I want to select DocumentType and related Document record with maximum value for Version. I tried following query:
from t in Documents
join tt in DocumentTypes on t.DocumentTypeId equals tt.Id
where tt.CreatedBy == 10
group t by t.DocumentTypeId into g
//let v = new {Version = g.Max( t => t.Version), TypeId =g.Key}
select new
{
Key = g.Key,
Version = g.Max(t=>t.Version),
Text = t.Text //ERROR AT t.Text
};
but it is giving me an error at following line:
Text = t.Text
The name 't' does not exist in the current context
I have tried g.Text also but it is not helping. Kindly help me to fix this query. I am trying this in LinqPad.

It seems that you need to retrieve a Document entity withing the same DocumentType which has the max value for the Version property. There is no need to group by ntext column.
After grouping you have groups of documents. The only thing left is to get one with the max Version value for each group. I'd order the group by this property in descending order, and get the first value:
from t in Documents
join tt in DocumentTypes on t.DocumentTypeId equals tt.Id
where tt.CreatedBy == 10
group t by t.DocumentTypeId into g
select g.OrderByDescending(t => t.Version).FirstOrDefault();
You could project the result Document entity into an anonymous type if you want.

Try
from t in Documents
join tt in DocumentTypes on t.DocumentTypeId equals tt.Id
where tt.CreatedBy == 10
orderby t.Version descending
group t by t.DocumentTypeId into g
select new
{
Key = g.Key,
Version = g.First().Version,
Text = g.First().Text
};

t already stand for something else.
Try this way
Version = g.Max(x=>t.Version),

Related

How can I turn SQL query that joins two columns and groups by count of one column and a column of each joined table into LINQ?

In my database, each URI has associated tags (Tag table) and each pageview (PageView table) is associated with someone viewing a particular page. I want to return a list of URIs that have the same tags as a given URI, by count of each URI that shares those tag(s). My SQL query looks like this:
select count(URI) as 'Count', p.URI, t.Name
from tracking.PageView as p
inner join Tracking.Tag as t on p.ID = t.PageViewID
where t.name in
(select t.Name
from tracking.PageView as p
inner join Tracking.Tag as t on p.ID = t.PageViewID
where p.URI = 'URI WE WANT TAGS OF'
)
and p.uri like '%/articles/%'
group by p.URI , t.name
order by Count desc
My apologies if the description is too vague for the query or if the query itself is rough. It was just the first one that worked. I've tried to separate the subquery into a variable and select values in that subquery, but it's been some time since I've used LINQ and I'm spinning wheels at this point.
The following is pretty much an exact translation of your current SQL query, which should get you started.
from p in tracking.PageView
join t in Tracking.Tag on p.ID equals t.PageViewID
where p.uri.Contains("/articles/")
&& (
from p2 in tracking.PageView
join t2 in Tracking.Tag on p2.ID equals t2.PageViewID
where p2.URI == "URI WE WANT TAGS OF"
select t2.name
).Contains(t.name)
group new { p, t } by new { p.URI, t.name } into g
orderby g.Count() descending
select new {
Count = g.Count(),
g.Key.URI,
g.Key.Name
}

group by linq to entity query to get one record having latest timestamp by joining tables

There are two tables and using linq query to get records. From second table, there can be multiple rows corresponding to first table with date timestamp... based on below query, I am getting all records, but is there a way we can get the row from second table which has latest timestamp ?
Table Parent
ID Name
1 M
2 N
3 O
4 P
5 Q
Table Child
Id fkID DateTime
1 2 01/12/2021 09:12:20
2 2 01/12/2021 09:13:20
3 2 01/12/2021 09:14:20
4 2 01/12/2021 09:15:20
5 2 01/12/2021 **09:16:20**
Linq query:
from p in Parent
join c in Child on p.id equals c.fkId into cJoin
from cJoin in cJoin.DefaultIfEmpty()
select new TempResponse
{
Id = p.Id,
Name = p.Name,
Date = c.Date
}
I am getting 10 records using above query but just need 5 records i.e. from child table instead of all 5 records, we need a record that has latest time stamp
**expected output**
1 M
2 N 01/12/2021 09:16:20
this record is 5'th record from child table because this one has latest date time stamp
( latest record )
3 O
4 P
5 Q
Is there any way we can use group by and get the record that has latest time stamp from second table ?
Assuming you have defined navigation properties for the FK, I would use a query like;
dbContext.Child.Where(c => c.Date == c.Parent.Child.Max(c2 => c2.Date))
I believe you can use:
var ans = from p in Parent
join cmax in (
from c in Child
group c by c.fkId into cg
select cg.OrderByDescending(c => c.Date).FirstOrDefault())
on p.Id equals cmax.fkId into cJoin
from c in cJoin.DefaultIfEmpty()
select new TempResponse {
Id = p.Id,
Name = p.Name,
Date = c != null ? c.Date : null
};
Note that the order of results seems to vary on SQL Server unless you add another orderby clause before the select to force an order.

How to get average of Categories if SubCategories are referenced in Result table?

I am having problems in writing a LINQ for calculating average value based on not directly referenced column.
Provide background and tell us what you've already tried.
I have following tables, now I want average marks based on category and subcategory, But fetching it through TopicId(FK) in Result table is going to be a big task.
(I tried and couldn't figure out how to do it)
Image having tables
How Can I get average marks for Category and subcategory?
This is the SQL query that you need:
select
c.Id as CategoryId,
c.CategoryName,
sc.Id as SubCategoryId,
sc.SubCategoryName,
AVG(r.Marks) as Average
from Result r
join Topic t on r.TopicId = t.Id
join SubCategory sc on t.SubCategoryId = sc.Id
join Category c on sc.CategoryId = c.Id
group by c.Id, c.CategoryName, sc.Id, sc.SubCategoryName
And this is the same with LINQ query syntax:
using (var db = new TopicContext())
{
// build the query
var query =
from r in db.Result
join t in db.Topic on r.TopicId equals t.Id
join sc in db.SubCategory on t.SubCategoryId equals sc.Id
join c in db.Category on sc.CategoryId equals c.Id
group r by new { c.Id, c.CategoryName, SubCategoryId = sc.Id, sc.SubCategoryName } into gr
select new
{
CategoryId = gr.Key.Id,
CategoryName = gr.Key.CategoryName,
SubCategoryId = gr.Key.SubCategoryId,
SubCategoryName = gr.Key.SubCategoryName,
Average = gr.Average(x => x.Marks)
};
// ToList() method executes the query, so we get the result on that line of code
var result = query.ToList();
}
If you have an existing database you can use EF Core to generate your own dbContext (in my example this is TopicContext class) with the help of Scaffold-DbContext command. You can find an example of this command on that page. To use this command you should install Microsoft.EntityFrameworkCore.Tools nuget package along with Microsoft.EntityFrameworkCore.SqlServer.

Convert sql to LINQ query for selection of multiple max columns in c#

SELECT MAX(sectionid) AS SectionId,MAX(displayorder) AS DisplayOrder,propertyid AS PropertyId,1 AS IsSpecSection FROM (
SELECT mp.SectionId ,mp.DisplayOrder ,mp.PropertyId FROM
ModelProperties mp
INNER JOIN PropertySections PS ON mp.SectionId =
ps.SectionId
WHERE ps.IsSpecSection = 1 )s
GROUP BY propertyid
I want to convert above query into LINQ, able to do it for selection of single max column but not for multiple.
I haven't tested the code you have to modify the code as you need
using (var dbContext = new YourEntityName())
{
var result = (from mp in dbContext.ModelProperties
join ps in dbContext.PropertySections on mp.SectionId equals ps.SectionId
where ps.IsSpecSection = 1
group a by new { propertyid } into g
select sectionid , MAX(displayorder)AS DisplayOrder,propertyid AS PropertyId, 1 AS IsSpecSection).ToList();
}
Max value in Linq select Within Innerjoin
You can use this code,
var list=(from mp in ModelProperties
join ps in PropertySections on mp.SectionId equals ps.SectionId
where ps.IsSpecSection == 1
group new { mp, ps } by new { mp.PropertyId } into mgrp
from grp in mgrp.DefaultIfEmpty()
select new
{
grp.mp.SectionId,
grp.mp.PropertyId,
grp.mp.DisplayOrder,
grp.ps.IsSpecSection
}).OrderByDescending(x=>x.SectionId).First();
This query helps you to retrieve ModelProperties rows that has matching SectionId in PropertySections and IsSpecSection has the value 1. Matching rows are then grouped by PropertyId. OrderByDescending sort the retrieved results in descending order of SectionId. First() retrieve the rows that has maximum SectionId for each PropertySections as the rows are sorted in descending order of SectionId.

Get only one (last) record in one-to-many join with linq-to-entities

I have the following in linq-to-entities
clientprojects = (from p in this.SAPMappingEntities.SAP_Master_Projects
join c in this.SAPMappingEntities.SAP_Master_ProjectPartners on c.project_no equals p.project_no
where c.partner_name.Contains(clientstring)
orderby p.start descending
select new ClientProjects { client = c.partner_name, location = c.city +", "+c.region, project_no = c.project_no, start_dt = p.start, end_dt = p.finish }).Take(50).ToList();
I would like change this query so that for each SAP_Master_Project only get the SAP_Master_ProjectPartners record which has the latest update_dt. How can I do this?
EDIT
There's a project table with a project number and project details including project start and end dates. There's a project partners table with the project partner number, name, project number, update date and other details.
SAP_MASTER_PROJECT
project_no
start
finish
SAP_MASTER_PROJECTPARTNERS
partner_no
project_no
partner_name
city
region
update_dt
When the user enters "ABC" into a text box, the info I want to return is the project number, project start date, project end date plus project partner name, city, and state from the last project partner record for the last 50 projects (based on start date) where the project partner name contains or is like "ABC".
I'm sure there's more than one way to do this, but his SQL gives me the results that I need:
SELECT TOP 50 p.project_no, p.start, p.finish, c.partner_name, c.city, c.region
FROM
(select pp.project_no, pp.partner_name, pp.city, pp.region
from SAP_Master_ProjectPartners pp
where pp.partner_name LIKE #clientstring AND pp.update_dt = (select max(pp1.update_dt)
from SAP_Master_ProjectPartners pp1
where pp1.project_no = pp.project_no)) c
join SAP_Master_Projects p
on (p.project_no = c.project_no)
ORDER BY p.start DESC
EDIT #2
That sql actually returns a few items which have the same update_dt, so I modified the sql to below. Still struggling to convert to linq.
SELECT TOP 50 p.project_no, p.start, p.finish, c.partner_name, c.city, c.region, c.update_dt, c.row_id
FROM SAP_Master_Projects p
join
(select pp.project_no, pp.partner_name, pp.city, pp.region, pp.update_dt, pp.row_id
from SAP_Master_ProjectPartners pp
where pp.partner_name LIKE #clientstring AND pp.row_id = (select TOP 1 row_id
from SAP_Master_ProjectPartners pp1
where pp1.project_no = pp.project_no order by update_dt DESC)) c
on (p.project_no = c.project_no) where p.active_flag = 1
ORDER BY p.start DESC
This query would probably be simpler if you defined an entity relationship between SAP_Master_Projects and SAP_Master_ProjectPartners so the join could be implicit instead of explicit.
Edit Since you can't do that, something like this might work (using let and doing a logical join within a where clause):
var clientProjects =
(
from p in entities.SAP_Master_Projects
let c = entities.SAP_Master_ProjectPartners
.Where(cl => cl.partner_name.Contains(clientstring)
&& cl.project_no == p.project_no
)
.OrderBy(cl => cl.update_dt) // Todo: Might need to be descending?
.FirstOrDefault()
where c != null
orderby p.start descending
select new ClientProjects
{
client = c.partner_name,
location = c.city + ", " + c.region,
project_no = c.project_no,
start_dt = p.start,
end_dt = p.finish
}
)
.Take(50)
.ToList()
;
It sounds like you're trying to come up with the following query:
SELECT *
FROM MasterProjects p
INNER JOIN (SELECT project_no,
partner_name
FROM ProjectPartners o
WHERE o.update_dt = (SELECT MAX(update_dt)
FROM ProjectPartners i
WHERE i.project_no = o.project_no)) c
ON p.project_no = c.project_no
AND p.partner_name = c.partner_name
I'm not entirely sure how to translate this in to LINQ but here is my best attempt:
var clientprojects =
from p in MasterProjects
join c in ProjectPartners on p.project_no == c.project_no
where c.partner_name == (from o in ProjectPartners
where o.project_no == c.project_no
and o.update_dt == (from i in ProjectParters
where o.project_no = i.project_no
select i.update_dt).Max()
select o.partner_name).First();
The above LINQ may not even compile, but hopefully it'll send you in the right direction.
I don't speak your language, sorry. But, for instance, in MySql you might add sort by update_dt DESC LIMIT 1 can you do that or somethign similar?

Categories

Resources