score:8
Just use this for grouping:
var results = transactions
.GroupBy(p => p.Buyer.UserID)
.Where(x=>x.Count()>1)
.Select(x=> new List<Transaction>(x));
This should produce your List<List<Transaction>
with repeated users from the main list.
LINQ is not really the right tool to modify a collection while iterating over it. If you really need it, do:
var results = transactions.ToList()
.GroupBy(p => p.Buyer.UserID)
.Where(x=>x.Count()>1)
.Select(x=>
{
foreach(var item in x)
transactions.Remove(item);
return new List<Transaction>(x);
});
Notice that a copy of transactions list is made to execute the query on ( transactions.ToList()
and remove operations are made on the original list. Hope that helped.
score:0
No you are getting an IEnumerable<IGrouping<TKey, TSource>>
See: MSDN Group By
So basically it returns a list of groups. The group contains the key where it is grouped on and the element which is the list of items which belongs to the group
The List
- Group 1
- Group Key
- Items (list of your objects)
- Group 2
- Group Key
- Items (list of your objects)
Nice explanation: https://www.udemy.com/blog/linq-group-by/
score:3
Try this :
var dic = _transactions.GroupBy(t => t.Buyer.UserID).ToDictionary(t => t.Key, t => t.ToList());
GroupBy returns a
IEnumerable<IGrouping<TKey, TSource>>
Each IGrouping<> contains as Key, the group criteria, and each IGrouping is enumerable (you list the grouped values)
Since, the question change :
Don't create anonymous types in the GroupBy delegates (no new {...}) Instead, directly target the property (because internally, GroupBy uses equality to group things, and since you create a new anonymous object, there will be nothing grouped, since each new object is a different one)
.GroupBy(o => o.Buyer.UserID, o.ToList)
Moreover,
Select(p => p)
does nothing, it's an identity function. It's useless.
If what you want is a List of List, I think you can directly use a Dictionary. (see the first part of my answer) You can enumerate a dictionary in the same way you enumerate a List. And add/remove/replace items in it.
The Dictionary returned will be of type :
Dictionary<int,List<Transactions>> dic =....
With Key = UserId, and for each key, a List of transactions. Moreover, it's really faster than a List of List, since you can directly access the transactions :
int userId = 42;
List<Transactions> transactions = dic[userId];
score:2
This has answer to your query.
var results = transactions.GroupBy(p => p.Buyer.UserID,
(key, g) => new { UserID = key, Cars = g.ToList() });
score:1
If I understand correctly,
You have a transaction list, and a transaction list of lists.
You need to group the latter, by the userId, where the value will be the transactions that correspond to that user.
If that's the case, I prepared a small sample that may help.
good luck
public class Transaction
{
public Buyer Buyer { get; set; }
}
public class Buyer
{
public int UserID { get; set; }
}
[TestMethod]
public void Test()
{
var t1 = new Transaction { Buyer = new Buyer { UserID = 1 } };
var t2 = new Transaction { Buyer = new Buyer { UserID = 2 } };
var t3 = new Transaction { Buyer = new Buyer { UserID = 3 } };
var t4 = new Transaction { Buyer = new Buyer { UserID = 1 } };
var t5 = new Transaction { Buyer = new Buyer { UserID = 3 } };
var tList1 = new List<Transaction> { t1, t2, t3 };
var tList2 = new List<Transaction> { t4, t5 };
var tLists = new List<List<Transaction>> { tList1 , tList2};
var result = tLists.
SelectMany(o => o).
GroupBy(o => new
{
UserId = o.Buyer.UserID
},
(key, items) => new
{
UserId = key.UserId,
Transactions = items.Select(p => p).ToList()
}).
ToList();
}
This is an updated solution to the edit by the author:
var transactions = new List<Transaction>
{
new Transaction { Buyer = new Buyer { UserID = 1 } },
new Transaction { Buyer = new Buyer { UserID = 2 } },
new Transaction { Buyer = new Buyer { UserID = 3 } },
new Transaction { Buyer = new Buyer { UserID = 1 } },
new Transaction { Buyer = new Buyer { UserID = 3 } }
};
var result = transactions.
Select(o => o).
GroupBy(o => new
{
UserId = o.Buyer.UserID
},
(key, items) => new
{
//UserId = key.UserId,
Transactions = items.Select(p => p).ToList()
}).
ToList();
Source: stackoverflow.com
Related Articles
- LINQ GroupBy return List as result
- StackOverflowException trying to return a Linq query result as a List through a WCF Service
- How to convert LINQ query result to list and return generic List?
- Code practice to handle empty result set in Linq to custom list
- LINQ GroupBy and project to type with both a list of its keys and result of ToDictionary
- LINQ function to return list but compiler says function doesn't return a value on all code path
- How to convert a list of anonymous object to string array using linq for return using Json Result in ASP.NET MVC
- How to return specific list result from linq to entities
- Compare list in LINQ and return 2 result objects
- C# return linq result as a list from a wcf service method then use it in aspx web forms page
- How to execute a linq query for each item in a list , use it in the where clause and return a collection from the result of each query?
- c# Linq or code to extract groups from a single list of source data
- Linq GroupBy with sort, take first, and return a list
- How to return a List type variable containing values from LINQ query result in C#.Net?
- LINQ query to return distinct field values from list of objects
- Return list using select new in LINQ
- Return list of specific property of object using linq
- compare two list and return not matching items using linq
- LINQ return items in a List that matches any Names (string) in another list
- How to get linq `ForEach` statement to return data on the method call being made for each list object?
- Linq to Objects - return pairs of numbers from list of numbers
- why is this linq query return a boolean and not the first result of the select?
- Return Linq Query into a List of KeyValuePair's
- LINQ with subselect and groupby to get only the latest version of each item in a list
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- c# linq GroupBy on the values of a List within a List
- Using LINQ on a List to return a subset list based on sequential dates
- Return one result from LINQ Query
- Return List with Maximum Count using Linq
- return a single list property List in Linq
- Find specific item on icollection
- How to do multiple groupings with a concrete class?
- Converting SQL Statement with Multiple Left Outer Joins and Various Clauses to LINQ
- How to convert a LINQ query to use lambda expressions
- LINQ C# query in VB - getting an error
- How to not add to the list, when the return of a LINQ IEnumerable is empty?
- ASP/MVC3 Razor Linq
- C# DataTable Filter Fastest Way
- MongoDB + NoRM- Concurrency and collections
- Adding SQL comment to Linq generated query so that it is visible in SQL profiler
- Getting error in linq to entity
- Haven't jumped on the Linq bandwagon, what are your reasons for not using linq?
- Join clause incorrect on multiple join LINQ
- How to build a LINQ to XML query with a conditional selection
- How to use LINQ Group By with Count
- LINQ and unique IDs
- Better way to sum
- Using Let in Lambda Expression while querying from a datatable
- How do you create Expression<Func<T, T>> given the property names of T to map?
- Linq get last ID inserted