Questions tagged [linq-group]
139 questions
53
votes
4 answers
Select multiple fields group by and sum
I want to do a query with linq (list of objects) and I really don't know how to do it, I can do the group and the sum but can't select rest of the fields.
Example:
ID Value Name Category
1 5 Name1 Category1
1 7 Name1 …

user2112420
- 955
- 5
- 11
- 26
17
votes
5 answers
LINQ - GroupBy a key and then put each grouped item into separate 'buckets'
I have a list of items as such:
public class Item
{
public int ItemId { get; set; }
public string ItemName { get; set; }
public int ListId { get; set; }
}
1 Test1 1
2 Test2 1
3 Test3 1
4 List 2
5 List2 2
6 Testing 3
7 Testing2 3
8…

Saxman
- 5,009
- 11
- 51
- 72
16
votes
3 answers
List Simple Group and Count?
I have a very simple List setup which contains lots of single characters per item (IE a foreach would console out to "a" "k" "p" etc)
What I'd like to do is be able to group the items and also count how many of each occurs so I'd get an…

tripbrock
- 952
- 4
- 13
- 28
16
votes
6 answers
Use LINQ to concatenate multiple rows into single row (CSV property)
I'm looking for the LINQ equivalent to the Sybase's LIST() or MySQL's group_concat()
It'll convert:
User Hobby
--------------
Bob Football
Bob Golf
Bob Tennis
Sue Sleeping
Sue Drinking
To:
User Hobby
--------------
Bob Football,…

John Paul Jones
- 689
- 4
- 10
- 17
10
votes
2 answers
Access all of the data after joining two tables and group them using linq
I have two tables
TableA
aId
aValue
TableB
bId
aId
bValue
I want to join these two tables via aId, and from there, group them by bValue
var result =
from a in db.TableA
join b in db.TableB on a.aId equals b.aId
group b by b.bValue into x
select…

sooprise
- 22,657
- 67
- 188
- 276
9
votes
5 answers
Lambda expression Group by in C#
I would like to group my LINQ query by ItemNumber and return the whole table with the total for Quantity.
Example:
ItemNumber - ItemName - Quantity
100 Item1 1
150 Item2 2
100 Item1 2
200 Item3 …

KLIM8D
- 582
- 1
- 8
- 25
6
votes
2 answers
Group objects with matching properties into Lists
I have a List of objects in C#. All the objects contain properties code1 and code2 (among other properties).
Example:
List -> object = id, name, code1, code2, hours, amount.
There are 31 possible code1 values.
There are 10 possible code2 values.
I…

Baxter
- 5,633
- 24
- 69
- 105
6
votes
5 answers
Linq query giving inappropriate output
I have two transaction tables named as ParentTransaction and ChildTransaction in which TransactionId of ParentTransaction will act as foreign to ChildTransaction of TransactionId.
Now I want to get all those TransactionId of ParentTransaction whose…

I Love Stackoverflow
- 6,738
- 20
- 97
- 216
6
votes
3 answers
LINQ Grouping by custom type not working
I have data I receive from a web service via HTTPWebRequest. After I parse it using NewtonSoft.Deserialize into a custom type (a simple class with public string properties), I want to manipulate this data using LINQ - more specifically, I want to…

Veverke
- 9,208
- 4
- 51
- 95
5
votes
2 answers
Why do changes made in foreach to a Linq grouping select get ignored unless I add ToList()?
I have the following method.
public IEnumerable- ChangeValueIEnumerable()
{
var items = new List
- (){
new Item("Item1", 1),
new Item("Item2", 1),
new Item("Item3", 2),
new…

vfabre
- 1,388
- 12
- 23
4
votes
3 answers
Using LINQ Group Joins in VB.NET
I'm trying to figure out how to use Group Joins in LINQ queries under VB.NET. For some reason, every example I seem to find on the syntax is just plain WRONG! At least, that's what my compiler keeps telling me. What is it exactly I'm doing wrong…

mclark1129
- 7,532
- 5
- 48
- 84
4
votes
1 answer
Group to key-value pair where value is a list of elements that share the same key
How to create a "complex" object from a list of fields
ex.:
public class Lines {
public string itemName;
public string locationName;
public string locationGeo;
}
List lines = new List(){
new Lines() {itemName= "item1",…

waldecir
- 376
- 2
- 8
- 21
4
votes
2 answers
C# LINQ GroupBy to convert a List to a group with one property as List of values
I have a list of objects list1
Name Value
N1 V100
N2 V101
N1 V102
N4 V103
N4 V104
I want to convert this into a grouped List
Name Values
N1 V100, V102
N2 V101
N4 V103, V104
When I use GroupBy I get the whole thing, (…

heyNow
- 866
- 2
- 19
- 42
4
votes
3 answers
Linq Grouping by Date (month)
I would like to know how I could group some data by month and sum a field.
For example I have a list of MyObjects(DateTimeField, AmountField).
I want to group by DateTimeField, but not entire date, just by month, and then sum the AmountField for…

user2112420
- 955
- 5
- 11
- 26
4
votes
2 answers
Grouping relatively close values using LINQ or loop
I guess this would be more maths than C#. I've got an array of float values, where most values belong to one of the few tightly packed ranges. Here's an example (Lower Limit=0,Upper Limit=612):
3.4,5.0,6.1,
144.0,144.14,145.0,147.0,…

dotNET
- 33,414
- 24
- 162
- 251