Linq select same id and remove other - c#

I need to filter this list.
I just want to have those person that have the same SalId and those must be at least two.
How do I do it?

var dupSalIdPersons = persons
.GroupBy(p => p.SalId)
.Where(g => g.Count() >= 2)
.SelectMany(g => g);

Related

How to order groupBy items

Having this Linq query which returns grouping of 4 DateTime:
IEnumerable<IGrouping<DateTime, QuoteSnapshotModel>> lista = (from q in quoteModeList
where q.QuoteTradeType == "Q"
select q).GroupBy(n => n.ExceriseDate);
How can I order the groups by DateTime and get only the first group?
meaning **List<QuoteSnapshotModel>**
Also, how can I get only the second List<QuoteSnapshotModel> (according to DateTime)
Try this:
for this you have to create a list<QuoteSnapshotModel> within class QuoteSnapshotModel.
int record = 1;
List<QuoteSnapshotModel> result =
quoteModeList
.Where(x => x.QuoteTradeType == "Q")
.GroupBy(x => x.ExceriseDate,
(a, b) => new QuoteSnapshotModel
{
ExceriseDate = a,
ListQuoteSnapshotModel = b.ToList()
})
.OrderByDescending(t => t.ExceriseDate)
.Skip(record - 1).Take(1).ToList();
Update
You can use only the first group
List<QuoteSnapshotModel> list =quoteModeList.Where(x=>x.QuoteTradeType =="Q")
.GroupBy(x => x.ExceriseDate)
.OrderBy(x=>x.Key)
.FirstOrDefault().Select(x=>x.QuoteTradeType).ToList();
You can use only the second group
List<QuoteSnapshotModel> list =quoteModeList.Where(x=>x.QuoteTradeType =="Q")
.GroupBy(x => x.ExceriseDate)
.OrderBy(x=>x.Key)
.Skip(1).First().Select(x=>x.QuoteTradeType).ToList()
How can I order the groups by DateTime
.OrderBy(g => g.Key) or .OrderBy(g => g.First().ExerciseDate)
and get only the first group?
.First() (or .FirstOrDefault() if it's possible there are 0 groups)
how can I get only the second List (according to DateTime)
.Skip(1).Take(1) or .ElementAt(1) or .Skip(1).First()
Putting it all together:
IEnumerable<IGrouping<DateTime, QuoteSnapshotModel>> lista = (
from q in quoteModeList
where q.QuoteTradeType == "Q"
select q
).GroupBy(n => n.ExerciseDate);
IList<QuoteSnapshotModel> firstQuote = lista.OrderBy(x => x.Key).Select(x => x.ToList()).FirstOrDefault();
IList<QuoteSnapshotModel> secondQuote = lista.OrderBy(x => x.Key).Skip(1).Select(x => x.ToList()).FirstOrDefault();

Linq - Group By Id, Order By and Then select top 5 of each grouping

Is there some way in linq group By Id, Order By descending and then select top 5 of each grouping? Right now I have some code shown below, but I used .Take(5) and it obviously selects the top 5 regardless of grouping.
Items = list.GroupBy(x => x.Id)
.Select(x => x.OrderByDescending(y => y.Value))
.Select(y => new Home.SubModels.Item {
Name= y.FirstOrDefault().Name,
Value = y.FirstOrDefault().Value,
Id = y.FirstOrDefault().Id
})
You are almost there. Use Take in the Select statement:
var items = list.GroupBy(x => x.Id)
//For each IGrouping - order nested items and take 5 of them
.Select(x => x.OrderByDescending(y => y.Value).Take(5))
This will return an IEnumerable<IEnumerable<T>>. If you want it flattened replace Select with SelectMany

c# and LINQ - convert IGrouping to List

I have the following code written to find common objects in a list of objects
https://dotnetfiddle.net/gCgNBf
..............................
var query = setOfPersons
.SelectMany(l => l.Select(l1 => l1))
.GroupBy(p => p.Id)
.Where(g => g.Count() == setOfPersons.Count);
After that, I need to convert "query" to a list of "Person" objects ( List ) to achieve something else.
I tried using "ToList()"... But it says:
" cannot convert IGrouping to a List ".
Can someone help me to fix it ?
Looking at your code it seems that what you are trying to achieve is to get the list of people that exist in each list. If so, you can use the following query:
var query = setOfPersons
.SelectMany(l => l.Select(l1 => l1))
.GroupBy(p => p.Id)
.Where(g => g.Count() == setOfPersons.Count)
.Select(x=>x.First()) // Select first person from the grouping - they all are identical
.ToList();
Console.WriteLine("These people appears in all set:");
foreach (var a in query)
{
Console.WriteLine("Id: {0} Name: {1}", a.Id, a.Name);
}
Here you select just a single item from each grouping, because they all are identical.

How to filter List With LINQ C#

I need to filter a List<Students> into StudentsWitHighestDebts.
The criteria is that only students where ZachetDidNotPass has maximum value and maximum-1 in all List<Students> are included in the result.
var StudentsWitHighestDebts = students
.Where(s => s.ZachetDidNotPass.(some condition))
.OrderBy(s => s.Name)
.ToList();
For example, given a list of students that have ZachetDidNotPass values 0 1 2 5 6 7. The resulting StudentsWitHighestDebts should only contain the students with 7 and 6 values in ZachetDidNotPass.
First option: take 2 highest debts and filter students by ZachetDidNotPass:
var highestDebts = students.Select(s => s.ZachetDidNotPass)
.OrderByDescending(p => p).Take(2).ToArray();
var studentsWitHighestDebts = students
.Where(s => highestDebts.Contains(s.ZachetDidNotPass))
.OrderByDescending(s => s.ZachetDidNotPass).ToList();
Second option - group by ZachetDidNotPass, sort groups by key descending, take top 2 groups and select students from groups
var studentsWitHighestDebts = students.GroupBy(s => s.ZachetDidNotPass)
.OrderByDescending(g => g.Key).Take(2)
.SelectMany(g => g).ToList();
And third option (take students with highest debt and highestDebt - 1)
var highestDebt = students.Max(s => s.ZachetDidNotPass);
var studentsWitHighestDebts = students
.Where(s => s.ZachetDidNotPass == highestDebt || s.ZachetDidNotPass == highestDebt - 1)
.OrderByDescending(s => s.ZachetDidNotPass).ToList();

LINQ TO SQL Limited select statement because of group by

I am looking to find all the dealers in the database that have a duplicate phone number. I then want to list them in a report with all the dealers information. I am having trouble getting my select statement to populate the dealer record attributes.
Such as dealer name address and so fourth. I am thinking it is because of the group by that is limiting it.
var dealers = _db.Dealers
.Where(x => x.Active)
.GroupBy(x => x.Phone)
.Select(x => new { Dealer = x.Key, Phone = x.Count()})
.Where(x => x.Phone > 1);
Edit:
The desired output would be list of each dealer. I want the dealers with the same phone number to appear next to each other. I don't want any records that are not duplicates based on phone number.
Just add the first item in the group to the last Select:
var dealers = _db.Dealers
.Where(x => x.Active)
.GroupBy(x => x.Phone)
.Select(x => new { Dealer = x.Key, Phone = x.Count(), FirstItem = x.First()})
.Where(x => x.Phone > 1);
the result will then have a FirstItem property
Or if you want all the items in a flat list you can apply the Where directly to the grouping:
var dealers = _db.Dealers
.Where(x => x.Active)
.GroupBy(x => x.Phone)
.Where(g => g.Count() > 1);
.SelectMany(g => g) // flat list of Dealers
You're not saying exactly what the expected output and the actual output is, so I don't know what you mean by 'having trouble'. But I spotted one potentially confusing thing:
You're grouping by the Phone (.GroupBy(x => x.Phone)).
So when you do new { Dealer = x.Key, ... the x.Key will refer to the phone number of this group.

Categories