score:3
Accepted answer
var indices = yourlist
.where(x => x.startswith("entry"))
.select(x => yourlist.indexof(x))
.tolist();
var groups = indices
.select((x,idx) => idx != indices.count - 1
? yourlist.getrange(x, indices[idx + 1] - x)
: yourlist.getrange(x, yourlist.count - x))
.tolist();
score:1
void main()
{
var data = new list<string>() { "entry1", "metadata1", "metadata2",
"entry2", "metadata3", "metadata4" };
pairwithgroup(data).groupby(t=>t.item1).dump();
}
private ienumerable<tuple<string,string>> pairwithgroup(ienumerable<string> input) {
string groupname = null;
foreach (var entry in input) {
if (entry.startswith("entry")) {
groupname = entry;
}
yield return tuple.create(groupname, entry);
}
}
score:1
key = entry1
value groupname
entry1 entry1
a entry1
b entry1
key = entry2
value groupname
entry2 entry2
c entry2
d entry2
e entry2
score:2
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
namespace consoleapplication1
{
class program
{
static void main(string[] args)
{
list<string> datas = new list<string>() { "entry1", "metadata1", "metadata2", "entry2", "metadata3", "metadata4" };
list<list<string>> grouped = new list<list<string>>();
int count = -1;
foreach (string e in datas)
{
if (e.startswith("entry"))
{
grouped.add(new list<string>());
grouped[++count].add(e);
}
else
{
grouped[count].add(e);
}
}
for (int i = 0; i < grouped.count; i++)
{
for (int j = 0; j < grouped[i].count; j++)
{
console.writeline(grouped[i][j]);
}
console.writeline();
}
}
}
}
score:2
var data = new list<string>() { "key1", "value1", "value2",
"key2", "value3", "value4" };
string workingkey = null;
data.tolookup(item => {
if(item.startswith("key"))
{
workingkey = item;
return string.empty; //use whatever here
}
return workingkey;
}).where(g => g.key != string.empty); //make sure to enumerate this if you plan on setting workingkey after this edit: where is enumerating so no need to enumerate again
Source: stackoverflow.com
Related Query
- Group Sequential List By Key
- Group by key and send values into list using LINQ
- Group to key-value pair where value is a list of elements that share the same key
- c# linq to xml XElement group by ancestor attribute to create list of entities collection with key
- c# - list of objects - group by - get distinct values by key - lambda / linq
- How to add List to a Key in a Linq to Sql Group By
- LINQ lambda OrderByDescending on group by key.count() returning list of string of group by key for chart legend sorting
- How to group inner list data based on a key in outer list in c# linq
- Display List of Objects in a group with self referencing foreign key
- Using Linq to group a list of objects into a new grouped list of list of objects
- Group List of Objects based on Property using Linq?
- Using LINQ to group a list of objects
- Linq Group on Multiple Fields - VB.NET, Anonymous, Key
- Distinct list of objects based on an arbitrary key in LINQ
- Group list by month
- LINQ Lambda, Group by with list
- Linq group key is not unique
- Group items in a list based on two properties using LINQ
- How can I group by a list of elements?
- Linq - group a list into pairs based on a property
- How to find the maximum value for each key in a List of Dictionaries using LINQ?
- Using LINQ on a List to return a subset list based on sequential dates
- Group list entries with LINQ
- Linq - Group by week on the List
- linq query group by in a list of strings
- LINQ Query: Determining if object in one list exists in another based on key
- Group a List based on uniqueness
- How to order a list by property and group the elements in HTML Razor?
- ModelState create List string with Key + ErrorMessage. (LINQ)
- How to merge group by list in LINQ
More Query from same tag
- Would appreciate assistance with .OrderBy and group/cross join
- Why my string value return as Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[System.String]
- How to bind linq query to a repeater?
- Casting String as DateTime in LINQ
- Remove all records with opposite sign
- Linq Query to join overlapping ranges and construct a new continuous range
- Method Chaining equivalent?
- LINQ Cast Enumerable to List of specific type
- Return filtered collection of results
- Subtract Two datetime (TimeStamp) and compare with interval in hours (int) in lambda query
- Add a Linq clause to a Iqueryable object
- Anonymous Type Name Collision
- Order list by date and sequence number
- Getting all items in a list that have a property set to the same value
- Custom where can't run
- group by query with not in sub query linq
- LINQ to SQL Basic column search doing OR / AND of a List<string> (VB.net)
- Include() only specific property
- OrderByDescending linq query
- linqDataSource bound to dynamic list
- Group by date range , count and sort within each group LINQ
- How use LINQ to find entities where a list of sub entities contains another list
- Beginner mistake in Linq Extension Methods with Entity Framework
- Using IEqualityComparer to find records
- Filter DataTable with any match of a string
- How to process a datatable that has been split into multiple equal datatables
- Linq two select statements, second one uses first ones result,
- C# Using Enumerable Range and Except with custom class to determine missing sequence number
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method
- XML replace value with Linq