Select and grouping based on different fields/integer - c#

Table in SQL:
Document Revision Version
-----------------------------------------------------
ABC 1 1
ABC 1 2
ABC 1 8
ABC 2 3
DocumentF 1 3
DocumentF 1 2
Expected output:
Document Revision Version
-----------------------------------------------------
ABC 1 8
ABC 2 3
DocumentF 1 3
Basically if there are two versions of the same revision, bring back the latest based on version.
I've tried the following:
var list = from document in documents
group document by document.Document
into groups
from g in groups
group g by g.Revision
into final
select final.OrderByDescending(d => d.Version).FirstOrDefault();
The above code produces the following:
Document Revision Version
-----------------------------------------------------
ABC 2 3
DocumentF 1 3

Using Linq , we can group by multiple properties like below
var list = from document in documents
group document by (document.Document, document.Revision)
into groups
select groups.OrderByDescending(d => d.Version).FirstOrDefault();

You need to GroupBy multiple columns, something like this:
var result = documents.GroupBy(c => new { c.Document, c.Revision })
.Select(c => new DocumentClass
{
Document = c.Key.Document,
Revision = c.Key.Revision,
Version = c.Max(d=>d.Version)
}).ToList();

Related

C# Linq using distinct with union

I have a table in db as :
Id Name Stream Version UId Tab Key Value CreatedOn CreatedBy
1 Name1 GOP 1 U1 Tab1 co 1 07/01/2018 S, Adam
2 Name1 GOP 1 U2 Tab1 co 1 07/03/2018 S, Adam
3 Name1 GOP 1 U3 Tab2 st 2 07/03/2018 S, Adam
4 Name1 GOP 2 OR Tab1 co 1 07/02/2018 P, Silver
5 Name2 GOP 1 OR Tab1 co 1 07/02/2018 P, Silver
6 Name3 GOP 0 OR1 Tab0 coe 1 07/02/2018 S, Adam
7 Name3 GOP 0 OR2 Tab1 coe 1 07/02/2018 S, Adam
8 Name2 LNT 3 NE Tab1 st 4 07/01/2018 P, Silver
9 Name2 LNT 3 NE1 Tab1 co 2 07/01/2018 P, Silver
10 Name2 LNT 2 NE2 Tab1 st 3 07/01/2018 P, Silver
11 Name2 LNT 0 NE Tab9 co 5 07/01/2018 R, Henry
12 Name3 TTE 0 TT Tab1 ee 2 07/02/2018 R. Henry
13 Name3 TTE 0 T1 Tab1 ee 2 07/02/2018 R. Henry
I want to write a query that would give me the highest version of set together with distinct version 0.
For this I wrote the query as but this does not get my desired output:
var data = response.GroupBy(x => new { x.Name, x.Stream })
.SelectMany(g => g.OrderByDescending(row => row.Version).Take(1)) //This gives highest version
.Union(response.Where(x => int.Parse(x.Version) == 0)) // This gives version 0
.OrderByDescending(o => o.CreatedOn).ToList();
Desired Output On UI
Id Name Stream Version CreatedOn CreatedBy
4 Name1 GOP 2 07/02/2018 P, Silver //This is shown as 2 is the highest version for Name1 & GOP combination
5 Name2 GOP 1 07/02/2018 P, Silver //This is shown as Name & Stream combination is different
6 Name1 GOP 0 07/02/2018 S, Adam //Version 0 is always shown - Combination of Name & Stream may or may not have more than one 0 version
8 Name2 LNT 3 07/01/2018 P, Silver //This is shown as 3 is the highest version for Name2 & LNT combination
11 Name2 LNT 0 07/01/2018 R, Henry //Version 0 is always shown
12 Name3 TTE 0 07/02/2018 R, Henry //Version 0 is always shown
On the UI I am trying to just show the trimmed down version of a set. When the user clicks on that set I would then show details for all the individual sets within the set.
Right now I only struck with how to update my query so I can get my desired result.
----Updated---
Right now what I got working is individual lists:
var data1 = response.GroupBy(x => new { x.Name, x.Stream})
.SelectMany(g => g.OrderByDescending(row => row.Version).Take(1))
.Where(x => int.Parse(x.Version) != 0)
.OrderByDescending(o => o.CreatedOn).ToList();
The above gives me all latest versions for a given name and stream.
var data2 = response.GroupBy(x => new { x.Name, x.Stream})
.Select(g => g.First())
.Where(x => int.Parse(x.Version) == 0)
.OrderByDescending(o => o.CreatedOn).ToList();
The above gives me all 0 versions for a given name and stream.
I think these individual lists works fine at the moment, but how to merge them.
Is there a way to join/merge these lists together so as to return just a single set. Or if there is a way to merge these 2 linq queries together.
---Updated-----
var set1 = response.GroupBy(x => new { x.Name, x.Stream})
.SelectMany(g => g.OrderByDescending(row => row.Version).Take(1))
.Where(x => int.Parse(x.Version) != 0).ToList();
var set2 = response.GroupBy(x => new { x.Name, x.Stream, x.Version})
.Select(g => g.First())
.Where(x => int.Parse(x.Version) == 0).ToList();
var setmerged = set1.Union(set2).OrderByDescending(o => o.CreatedOn).ToList();
Got it working by above not sure if this is an clean solution.
Wrong use of DISTINCT
This doesn't make sense. DISTINCT is used to filter duplicate rows, which they aren't as only the version number is the same. Where should the DBS know from, which CreatedOn value it should use?
Better solution
What you want is to use GROUP BY in order to group everything with the same version value. Be aware that you'll need to use an aggregate function like MAX()on the other columns in order to use it correctly.
So from the looks of your updated question, you want the top one of each distinct version, name and stream.
According to the dataset you've provided, and the result sets you want, there is no need to do anything special for the 0 version because the conditions for grabbing the distinct version should include the 0 version as well.
You haven't really specified what the order of the version should be so for now I'll assume that the one that was most recently updated is the one you want
response.GroupBy(x => new { x.Name, x.Stream, x.Version })
.SelectMany(g => g.OrderByDescending(row => row.CreatedOn).Take(1))
.ToList();

Sort IQueryable by specific order

I have a table that I want to pull items by name in a certain order and by date.
id name date
1 XYZ 1:30
2 ABC 1:40
3 LMNOQ 1:50
4 ABC 1:20
I created the order
List<string> itemNames = new List<string>();
documentOrder.Add("XYZ");
documentOrder.Add("ABC");
documentOrder.Add("LMNOQ");
Then pulled the data, I think order will stay intact.
var myTable = _context.TheTable.Where(x => itemNames.Contains(x.id));
myTable data will look like
id name date
1 XYZ 1:30
2 ABC 1:40
4 ABC 1:20
3 LMNOQ 1:50
Now I need to sort by dates without messing up the name order. I need it look like this.
id name date
1 XYZ 1:30
2 ABC 1:20
4 ABC 1:40
3 LMNOQ 1:50
var myTable = _context.TheTable.Where(x => itemNames.Contains(x.id))
.OrderBy(x => x.name).ThenBy(x => x.date);
When using an IQueryable you can get it ordered as you want (by name, then by date... which looks a bit like a time) with multiple ordering statements.
var orderedQueryable = queryable.OrderBy(q => q.Name).ThenBy(q => q.Date);

How to do select multiple records based on max version using Entity Framework

I have records such like this
id name number version
---------------------------
1 NewYork 1 1
2 LosAngeles 1 2
3 Seatle 1 3
4 Toronto 2 1
5 Ottawa 2 2
I want to select only the records with highest version within the same number
So I wrote query like this
SELECT *
FROM city c
WHERE c.[version] = (SELECT Max([version])
FROM [city] c2
WHERE c2.number = c.number)
and it would return
id name number version
---------------------------
3 Seatle 1 3
5 Ottawa 2 2
How do I write that in linq with Entity Framework?
db.cities.where(c => c.version == (????))
I don't know how Entity Framework would work for this.
Use GroupBy to group by the number and then OrderBy the record with the highest version:
var result = db.cities.GroupBy(item => item.number)
.Select(grouping => grouping.OrderByDescending(item => item.version)
.First());
var maxVersionCities = db.cities
.GroupBy(c => c.number)
.Select(grp => grp
.OrderByDescending(c => c.version)
.First())
.SelectMany(grp => grp);

Grouping records that haven't groups values

Please consider this records:
Id Week Value
-----------------------------
1 1 1000
2 1 1200
3 2 800
4 3 1800
5 3 1100
6 3 1000
I want to group records for 4 weeks but we haven't record for week 4.For Example:
Week Count
---------------------
1 2
2 1
3 3
4 0
How I can do this with linq?
Thanks
First you need an array of weeks then this query might help
var weeks = new List<int>{1,2,3,4}
var q = from w in weeks
join rw in (
from r in table
group r by r.Week into g
select new {week = g.Key, count = g.Count()}) on w equals rw.week into p
from x2 in p.DefaultIfEmpty()
select new {w, count = (x2 != null ? x2.count : 0)};
online result in .net fiddle
You can try
var result = Enumerable.Range(1, 4)
.GroupJoin(table,
week => week,
record => record.Week,
(week, records) => new { Week = week, Count = records.Count() });
As suggested by jessehouwing, the Enumerable.Range will return the possible week numbers to be used as left outer keys within the join.
GroupJoin will then accept as parameters
A lambda/delegate/method that returns the left outer key
A lambda/delegate/method that extracts the right key from your table.
A lambda/delegate/method that builds an item of the result.
Regards,
Daniele.

EF Query Help - Grouping and Sub queries

I have data table (DOCs, which is the DBSet in my context) with below data
ID Code Rev
1 A1 1
2 A1 2
3 A1 3
4 A3 1
5 A2 1
6 A2 2
I need to select the records which has a records for each Code which has the highest Rev. My expected result is
ID Code Rev
3 A1 3
6 A2 2
4 A3 1
The ID column is the PK of the table and Code+Rev is unique.
Note: There are other fields in the table which i need to get for the result. Ideal would be to get a iqueryable (Doc is the model class), i was think of selecting the ID within an inner query and then use that to get the iqueryable of docs.
Thanks in Advance
Try this:
var res = from r in DOCs
where (from c in DOCs
group c by c.Code into g
select new {
localCode = g.Key,
localRev = g.Max(t => t.Rev)
}).Any(x => x.localCode == r.Code && x.localRev == r.Rev)
select r;
res is IQueryable.

Categories