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: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();
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: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];
Source: stackoverflow.com
Related Query
- 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
More Query from same tag
- Orderby() not ordering numbers correctly c#
- How to update my model code using LINQ
- LINQ and various joining sample
- Join Subquery result in Linq
- How should I treat LINQ DB Context objects?
- Outer join with Linq causing "GroupJoin" error
- C# why is duplicating object record when persist?
- Asp.net linq query
- Populate list with missing dates LINQ
- LINQ-To-XML Query on Mulitple Identical Elements
- Prevent Entity Framework from casting
- How do I sort a list based on its child object attributes?
- Linq 2 SQL: composite primary key
- Linq List comparing
- Linq Simple query error
- Partial Record Updates with Linq to SQL and MVC
- Argument Exception Was Caught
- XElement to List in c#
- Find 1st # in Sequence
- CS8602 – Possible null reference – How to resolve properly this warning?
- Querying nested properties in mongo with c# and linq
- C# Employee Manger Relationship in model class
- How do I convert SQL Query with Ccase Switch to Linq Lambda
- feature an item based on the date
- How to select Max StartDate in Linq
- Linq to NHibernate: 'System.Linq.EnumerableQuery`1[Entity]' cannot be converted
- Need to get the sum from the joined column in LINQ
- Subsonic 3 LINQ Projection issue, fixed or no?
- Error handling for Tasks inside Task.WhenAll
- Linq to XML Read XPath (Read all nodes)