How to get distinct with highest value using Linq - c#

Let's say I have following data:
Name Priority
A 3
A 5
B 1
C 1
C 3
C 2
I want to get list of distinct names with highest priority, so the result would look like:
Name Priority
A 5
B 1
C 3
How can I use Linq to do that?

var query = yourData
.GroupBy(x => x.Name,
(k, g) => g.Aggregate((a, x) => (x.Priority > a.Priority) ? x : a));
// and a quick test...
foreach (var result in query)
{
Console.WriteLine(result.Name + " " + result.Priority);
}

Here's an alternative approach:
var items = new List<Tuple<string, int>>()
{
Tuple.Create("A", 3),
Tuple.Create("A", 5),
Tuple.Create("B", 1),
Tuple.Create("C", 1),
Tuple.Create("C", 3),
Tuple.Create("C", 2)
};
var results = items.GroupBy(i => i.Item1)
.SelectMany(g => g
.Where(i => i.Item2 == g.Max(m => m.Item2)))
.Distinct();
Or if you prefer using the C# LINQ syntax:
results = (from item in items
group item by item.Item1 into groupedItems
let maxPriority = groupedItems.Max(item => item.Item2)
from element in groupedItems
where element.Item2 == maxPriority
select element).Distinct();

Another simple approach without aggregating
var listNP = new List<NP>()
{
new NP() {name="A",priority=3},
new NP() {name="A",priority=5},
new NP() {name="b",priority=1},
new NP() {name="b",priority=1},
new NP() {name="c",priority=3},
new NP() {name="c",priority=2},
};
var np = listNP.GroupBy(x => x.name).Select(y => new
{
name = y.Key,
max = y.Max(x=>x.priority)
}).ToList();
Update:
var np = listNP.GroupBy(x => x.name)
.Select(y => y.OrderByDescending(z => z.priority).First()).ToList();

Related

Linq - Get a list and sort it by a list of string values

I have a list of guids as string:
This is how i retrive my list of string guids:
List<string> p0 = ctx.PrProjectRating.Select(k => k).GroupBy(g => new { g.PrPIdG }, (key, group) => new { sumR = group.Sum(k => k.PrValue), pidG = key.PrPIdG }).Select(t => t.pidG).ToList();
Now i have another list that contains a field called pidG but this list needs to be ordered by the list of guid strings above.
How do i achiveve this.
i tried:
List<PProject> p = p.OrderBy(c => p0.Contains(c.PIdG)).ToList();
but still the list is not ordered by the string guids in the first list "p0"
You have to do join here
List<string> p0 = ctx.PrProjectRating
.Select(k => k)
.GroupBy(g => new { g.PrPIdG }, (key, group) =>
new { sumR = group.Sum(k => k.PrValue), pidG = key.PrPIdG })
.Select(t => t.pidG).ToList();
var result = p0.Join(p, x => x, c => c.PIdG, (x, c) => c)
.ToList()

Preserve order with linq after groupby and selectmany

Is there a way to preserve the order after this linq expression?
var results =
DateList
.GroupBy(x => x.Date.Subtract(firstDay).Days / 7 + 1)
.SelectMany(gx => gx, (gx, x) => new {Week = gx.Key,DateTime =x,Count = gx.Count(),});
I found this Preserving order with LINQ , but I'm not sure if its the GroupBy or SelectMany casing the issues
Yes, if you first select your DateList and combine it with an index, using an overload of .Select that uses a delegate with a second (int) parameter that is called with the index of the items from the sequence :
DateList
.Select((dateTime, idx) => new {dateTime, idx})
.GroupBy(x => x.dateTime.Date.Subtract(firstDay).Days / 7 + 1)
...and persist the value through the linq chain
.SelectMany(gx => gx, (gx, x) => new {Week = gx.Key,
DateTime = x.dateTime,
Count = gx.Count(),
x.idx})
...then use it to re-order the output
.OrderBy(x => x.idx)
...and strip it from your final selection
.Select(x => new {x.Week, x.DateTime, x.Count});
then you can maintain the same order as the original list.
Solution of #spender is good, but can it be done without OrderBy? It can, because we can use the index for direct indexing into array, but it would not be one linq query:
var resultsTmp =
DateList.Select((d, i) => new { d, i })
.GroupBy(x => x.d.Date.Subtract(firstDay).Days / 7 + 1)
.SelectMany(gx => gx, (gx, x) => new { Week = gx.Key, DateTime = x.d, Count = gx.Count(), x.i })
.ToArray();
var resultsTmp2 = resultsTmp.ToArray();
foreach (var r in resultsTmp) { resultsTmp2[r.i] = r; };
var results = resultsTmp2.Select(r => new { r.Week, r.DateTime, r.Count });
It looks a bit complex. I would probably do something more straightforward like:
var DateList2 = DateList.Select(d => new { DateTime = d, Week = d.Subtract(firstDay).Days / 7 + 1 }).ToArray();
var weeks = DateList2.GroupBy(d => d.Week).ToDictionary(k => k.Key, v => v.Count());
var results = DateList2.Select(d2 => new { d2.Week, d2.DateTime, Count = weeks[d2.Week] });

How to select non-distinct elements along with their indexes

List<string> str = new List<string>() {
"Alpha", "Beta", "Alpha", "Alpha", "Gamma", "Beta", "XYZ" };
Expected output:
String | Indexes
----------------------------
Alpha | 0, 2, 3
Beta | 1, 5
Gamma and XYZ are distinct so, they are ignored.
I've done this by comparing the strings manually. Would it be possible to do it using LINQ in more easier way?
foreach (var grp in
str.Select((s, i) => new { s, i })
.ToLookup(pair => pair.s, pair => pair.i)
.Where(pair => pair.Count() > 1))
{
Console.WriteLine("{0}: {1}", grp.Key, string.Join(", ", grp));
}
Something like this should work:
var elements = str
.Select((Elem, Idx) => new {Elem, Idx})
.GroupBy(x => x.Elem)
.Where(x => x.Count() > 1);
If you want to get a Dictionary<string,List<int>> having the duplicated string as key and the indexes as value, just add
.ToDictionary(x => x.Key, x => x.Select(e => e.Idx).ToList() );
after Where()
You can get the non-distinct strings by grouping, then you can get the index for each non-distinct string and group them to create an array for each string:
var distinct = new HashSet<string>(
str.GroupBy(s => s)
.Where(g => g.Count() > 1)
.Select(g => g.Key)
);
var index =
str.Select((s, i) => new {
Str = s,
Index = i
})
.Where(s => distinct.Contains(s.Str))
.GroupBy(i => i.Str).Select(g => new {
Str = g.Key,
Index = g.Select(s => s.Index).ToArray()
});
foreach (var i in index) {
Console.WriteLine("{0} : {1}", i.Str, String.Join(", ", i.Index.Select(n => n.ToString())));
}
Output:
Alpha : 0, 2, 3
Beta : 1, 5

Convert to LINQ lambda expression

Simple line:
var x = (from a in arr select a).First();
Console.WriteLine(“First" + x);
How to convert to Lambda expression?
So you want to convert the LINQ query from using query syntax to plain extension method calls?
// var first = (from a in arr select a).First();
var first = arr.First();
// var last = (from a in arr select a).Last();
var last = arr.Last();
// var filtered = (from a in arr where a == 10 select a).First();
// there are a couple of ways to write this:
var filtered1 = arr.Where(a => a == 10)
.First();
var filtered2 = arr.First(a => a == 10); // produces the same result but obtained differently
// now a very complex query (leaving out the type details)
// var query = from a in arr1
// join b in arr2 on a.SomeValue equals b.AnotherValue
// group new { a.Name, Value = a.SomeValue, b.Date }
// by new { a.Name, a.Group } into g
// orderby g.Key.Name, g.Key.Group descending
// select new { g.Key.Name, Count = g.Count() };
var query = arr1.Join(arr2,
a => a.SomeValue,
b => b.AnotherValue,
(a, b) => new { a, b })
.GroupBy(x => new { x.a.Name, x.a.Group },
x => new { x.a.Name, Value = x.a.SomeValue, x.b.Date })
.OrderBy(g => g.Key.Name)
.ThenByDescending(g => g.Key.Group)
.Select(g => new { g.Key.Name, Count = g.Count() });
When you have an expression of the form (from y in x select y), you can almost always write x instead.

How do I find a subset of items in two sets of data that partially differ?

I am trying to get the subset of items in dataA that are in dataB, and have different values of property c. The properties a and b can be used as an index, so I have tried to filter out only the useful pairs then check to see if they have a different c value.
This is the linq expression I came up with, and it does work, but It seems like there has to be a better/faster way of finding this subset.
var itemsInBoth = from item in dataA
from item2 in dataB
where item.a == item2.a && item.b == item2.b
select new
{
first= item,
second = item2
};
var haveDifferentC = from item in itemsInBoth
where item.first.c != item.second.c
select item.first;
Based on the answer provided by David B, I eventually settled on a slightly modified version of his method. Although the differences are minor, I thought I would share this, primarily to show a version that for those (like me) that prefer the expressive syntax.
Also, instead of grouping, I decided to use an anonymous key/value pair to simplify the structure.
var dictA = (from item in dataA
select new
{
key = CreateIndexValue(item.a, item.b),
value = item
}).ToDictionary(kv => kv.key, kv => kv.value);
var dictB = (from item in dataB
select new
{
key = CreateIndexValue(item.a, item.b),
value = item
}).ToDictionary(kv => kv.key, kv => kv.value);
var filesInBoth = from item in dictA
where dictB.ContainsKey(item.Key)
select new
{
itemA = dictA[item.Key],
itemB = dictB[item.Key]
};
var differentSize = from item in filesInBoth
where item.itemA.c!= item.itemB.c
select item.itemA;
Faster? What you have there is O(n^2). Each item in the first list will fully iterate the items in the second list. You need to remove the redundant iteration in that join. One way to do that is to use another structure to do O(1) lookups for matchs.
Here's some untested (unchecked) code:
var dictionaryA = dataA
.GroupBy(item => new {a = item.a, b = item.b})
.ToDictionary(g => g.Key, g => g.ToList());
var dictionaryB = dataB
.GroupBy(item => new {a = item.a, b = item.b})
.ToDictionary(g => g.Key, g => g.ToList());
var results = dictionaryA
.Where(g1 => dictionaryB.ContainsKey(g1.Key))
.Select(g1 => new {g1 = g1, g2 = dictionaryB[g1.Key]})
.SelectMany(pair =>
pair.g1.SelectMany(item1 =>
pair.g2
.Where(item2 => item2.c != item1.c)
.Select(item2 => new {item1, item2})
)
);
Here's a simplified version if a,b pairs are unique in each list.
var dictionaryA = dataA
.ToDictionary(item => new {a = item.a, b = item.b}, item => item);
var dictionaryB = dataB
.ToDictionary(item => new {a = item.a, b = item.b}, item => item);
var results = dictionaryA
.Where(e1 => dictionaryB.ContainsKey(e1.Key))
.Select(e1 => new {i1 = e1.Value, i2 = dictionaryB[e1.Key]})
.Where(pair => pair.i1.c != pair.i2.c);

Categories

Resources