I need to do the following, I have a List with a class which contains 2 integer id and count
Now I want to do the following linq query:
get the sum of the count for each id
but there can be items with the same id, so it should be summerized e.g.:
id=1, count=12
id=2, count=1
id=1, count=2
sould be:
id=1 -> sum 14
id=2 -> sum 1
how to do this?
Group the items by Id and then sum the Counts in each group:
var result = items.GroupBy(x => x.Id)
.Select(g => new { Id = g.Key, Sum = g.Sum(x => x.Count) });
Try it ,
.GroupBy(x => x.id)
.Select(n => n.Sum(m => m.count));
The following program...
struct Item {
public int Id;
public int Count;
}
class Program {
static void Main(string[] args) {
var items = new [] {
new Item { Id = 1, Count = 12 },
new Item { Id = 2, Count = 1 },
new Item { Id = 1, Count = 2 }
};
var results =
from item in items
group item by item.Id
into g
select new { Id = g.Key, Count = g.Sum(item => item.Count) };
foreach (var result in results) {
Console.Write(result.Id);
Console.Write("\t");
Console.WriteLine(result.Count);
}
}
}
...prints:
1 14
2 1
Related
I was looking to get items from ListA, where the value of Id is same in both of the lists, and the count of Id must be more than 1 in list A or list B
var items = itemsA.Where(x => itemsB.Select(y => y.Id == x.Id).Count() > 1);
This gives me the result where same Ids in itemsB is more then 1, I want to use a or condition to check for the same counter in itemsA
Eg 1:
ListA=[{"id"=1,"name="abc"},{"id=1, "name"="def"}]
ListB=[{"id=2","name="xyz"}, {"id=1, "name"="mno"}]
Should return [{"id"=1,"name="abc"},{"id=1, "name"="def"}] because id =1 exists in listB and the count of id with value 1 in listA is more then 1.
Eg 2:
ListA=[{"id"=2,"name="abc"},{"id=1, "name"="def"}]
ListB=[{"id=1","name="xyz"}, {"id=1, "name"="mno"}]
should return {"id=1, "name"="def"} because common id in both list is 1 and the count of id with value 1 in ListB is more then 1.
I am not certain this is the best solution, but as far as I've understood the question, it should be a solution.
Assuming you have an Item class as follows:
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
}
and define itemsA and itemsB as List<Item>s, you can first find all Ids that are present in both lists, then select the applicable items from itemsA based on occurrence of each Id in either list:
IEnumerable<int> idsInBothItemLists = itemsA
.Select(a => a.Id)
.Intersect(itemsB.Select(b => b.Id))
.Distinct();
List<Item> items = itemsA
.Where(a => idsInBothItemLists.Contains(a.Id))
.GroupBy(a => a.Id)
.Where(gr =>
gr.Skip(1).Any() ||
itemsB.Where(b => b.Id == gr.Key).Skip(1).Any())
.SelectMany(gr => gr.Select(item => item))
.ToList();
(.Skip(1).Any() serves the same purpose as .Count() > 1 in your original code; it simply checks whether there are any items left after skipping the first item.)
Printing the output from the suggested population of itemsA and itemsB
foreach (var entry in items)
{
Console.WriteLine(entry.Id + " " + entry.Name);
}
e.g. for input
var itemsA = new List<Item>
{
new Item { Id = 1, Name = "abc" },
new Item { Id = 3, Name = "def" },
new Item { Id = 1, Name = "ghi" },
new Item { Id = 2, Name = "jkl" }
};
var itemsB = new List<Item>
{
new Item { Id = 2, Name = "xyz" },
new Item { Id = 2, Name = "jkl" },
new Item { Id = 1, Name = "mno" },
new Item { Id = 3, Name = "pqr" }
};
gives
1 abc
1 ghi
2 jkl
The extention method below does not have Distinct and Count
public static IEnumerable<Something> ToFilterModel(this IEnumerable<Product> products)
{
var v = products
.SelectMany(x => x.ProductVariants)
.GroupBy(x => x.OptionId)
.Select(x => new
{
Id = x.Key.ToString(),
Items = x.Select(x => new Item { Id = x.ValueId, Text = x.Value.OptionValue })
});
return v;
}
Given the input below it should return 2 Items rows and not 3, since i am interested for ValueIds
and also Count by ValueIds
how should i modify it?
More spesifically it should return items with rows 1 and 2 and also
Count equal to 1 for the first row and Count equal to 2 for the second row.
You could group by ValueId the grouped options, like :
Items = x
.GroupBy(y => y.ValueId)
.Select(z => new Item { Id = z.Key, Text = z.First().Value.OptionValue, Count = z.Count() })
The result will be :
{
"Id":1,
"Items":[
{
"Id":1,
"Text":"text1",
"Count":1
},
{
"Id":2,
"Text":"text2",
"Count":2
}
]
}
NOTE : the Text is the count of grouped value ids.
The whole code :
var v = products
.SelectMany(x => x.ProductVariants)
.GroupBy(x => x.OptionId)
.Select(x => new
{
Id = x.Key.ToString(),
Items = x
.GroupBy(y => y.ValueId)
.Select(z => new Item { Id = z.Key, Text = z.First().Value.OptionValue, Count = z.Count() })
});
I can split an array into smaller chunks.
public class Item{
public string Name {get; set;}
public bool IsUnique {get;set;}
}
public static void Main()
{
Random r = new Random();
var source = new[] {
new Item { Name = "Item-1", IsUnique = true},
new Item { Name = "Item-2", IsUnique = true},
new Item { Name = "Item-3", IsUnique = true},
new Item { Name = "Item-4"},
new Item { Name = "Item-5"},
new Item { Name = "Item-6"},
new Item { Name = "Item-7"},
new Item { Name = "Item-8"},
new Item { Name = "Item-9"}
};
var chunkSize = 3;
var result = source
.OrderBy(a => r.Next())
.Select((x, i) => new { Index = i, Item = x })
.GroupBy(s => s.Index / chunkSize)
.Select(g => g.ToList())
.ToList();
foreach(var item in result)
{
Console.WriteLine("Chunk: "+ (result.IndexOf(item)+1));
Console.WriteLine("-----------------------------------");
foreach(var x in item)
{
Console.WriteLine(x.Item.Name);
}
Console.WriteLine();
}
}
The result is like this:
Chunk: 1
-----------------------------------
Item-2
Item-3
Item-8
Chunk: 2
-----------------------------------
Item-5
Item-9
Item-7
Chunk: 3
-----------------------------------
Item-6
Item-4
Item-1
But if IsUniquer property of an item is true, they can not be in same chunk. For example above, Chunk-1 contains item-2 and item-3 .
Can I do this using linq?
UPDATE:
If chunk size is 3, only 3 item may be IsUnique=true.
Split your source array into two groups: those items which is unique and rest. Then iterate through each element in unique collection and take chunkSize - 1 from nonUnique collection. Take a look at thhis code:
var unique = source.Where(x => x.IsUnique);
var nonUnique = source.Where(x => !x.IsUnique)
.OrderBy(x => r.Next())
.ToList();
var result = unique.Aggregate(
(list: new List<List<Item>>(), items: nonUnique),
(c, n) =>
{
var next = c.items.Take(chunkSize - 1).ToList();
next.Add(n);
c.items.RemoveRange(0, chunkSize - 1);
c.list.Add(next.OrderBy(x => r.Next()).ToList());
return (c.list, c.items);
}).list;
I have a LogData List and which is designed like below.
public class LogDataEntity
{
public string IndexPattern;
public int Type;
public LogDataEntity(string pattern , int type)
{
IndexPattern = pattern;
Type = type;
}
}
List<LogDataEntity> list = new List<LogDataEntity>();
list.add(new LogDataEntity("1,2,9,10", 2));
list.add(new LogDataEntity("1,10", 1));
list.add(new LogDataEntity("1,2,3", 2));
list.add(new LogDataEntity("3,9,10", 3));
list.add(new LogDataEntity("9,10", 2));
list.add(new LogDataEntity("9,10", 2));
And i want the result like below.
[Index] [Type] [Count]
10 : 2 3
9 : 2 3
1 : 2 2
3 : 1 2
2 : 2 2
3 : 3 1
9 : 3 1
10 : 3 1
1 : 1 1
I want to group by and count not only splited string(indexpattern) but also
type too. And i want to count and show them by OrderByDescending(Count).
I think There is multiple group by.
How should i do this with Linq?
You can use SelectMany to create list of (Index, Type) pairs, then group by and count to do the rest:
var pairs = data.SelectMany(x => x.IndexPattern
.Split(",")
.Select(y => new {Index = y, Type = x.Type});
var res = from p in pairs
group p by new { p.Index, p.Type } into grp
select new {
Index = grp.Key.Index,
grp.Key.Type,
grp.Count()
};
(An order by clause can be added before the final Select as required.)
You've, probably, stuck in SelectMany; all the other commands are quite evident:
var result = list
.SelectMany(record => record
.IndexPattern
.Split(',')
.Select(item => {
index = item,
type = record.Type,
}))
.GroupBy(item => item)
.OrderByDescending(chunk => chunk.Count())
.Select(chunk => $"{chunk.index,-10} : {chunk.type,-10} {chunk.Count()}");
Console.WriteLine(string.Join(Environment.NewLine, result));
This is improve version or previous answers.
var pairs = Logs.SelectMany(x => x.IndexPattern.Split(',').Select(y => new {
Index = y, Type= x.Type }));
var pairs2 = (from p in pairs group p by p into grp select new { Index =
grp.Key.Index, Reason = grp.Key.Type, Count = grp.Count() }
).OrderByDescending(p => p.Count);
foreach (var i in pairs2)
{
//print with i
}
If I have a List<MyType> as so, with each line representing an item in the collection:
{{ Id = 1, Year = 2010 },
{ Id = 1, Year = 2009 },
{ Id = 1, Year = 2008 },
{ Id = 2, Year = 2010 },
{ Id = 2, Year = 2009 },
{ Id = 2, Year = 2008 }}
I wish to retrieve a collection from this collection of the most recent item for each Id. What will the Linq for this look like?
Desired output:
{{ Id = 1, Year = 2010 },
{ Id = 2, Year = 2010 }}
I have a naiive implementation using a second list variable and a foreach loop, but it's inefficient.
//naiive implementation "p-code"
//...
var mostRecentItems = new List<MyType>();
var ids = collection.Select(i => i.Id).Distinct();
foreach(var id in ids)
{
mostRecentItems.Add(collection.Where(i => i.Id == id).OrderByDescending().First);
}
return mostRecentItems;
Most simply:
var mostRecentById = from item in list
group item by item.Id into g
select g.OrderByDescending(x => x.Year).First();
Group by id, then select the first item in each group ordered in a descending fashion.
var mostRecentItems = collection.GroupBy( c => c.Id )
.Select( g => g.OrderByDescending( i => i.Year ).First() );
or more simply still:
var result = list
.GroupBy(i => i.Id)
.Select(g => new {Id = g.Key, Year = g.Max(y => y.Year)});