C
C#2y ago
BenMcLean

❔ Return groups of results using LINQ?

I'm iterating through a set of data with LINQ. The data's broken up into sections, where there's a "1" in the "NewSection" column on the first row of a new section, otherwise there's a "0" in that column. I want to make a function that returns an Enumerable of Enumerables of my data rows, where the inner enumerable is all the entries in each section. How would I construct my loop for this?
11 Replies
BenMcLean
BenMcLeanOP2y ago
The specific application is actually chapter and verse divisions in the text of a book. So there's a row for each verse, a "1" in the "IsABook" column at the start of each book, a "1" in the "IsAChapter" column at the start of each chapter, etc. Here's what I figured out:
public IEnumerable<IEnumerable<Row>> Chapters(IEnumerable<Row> book)
{
bool first = true;
List<Row> verses = new List<Row>();
foreach (Row verse in book)
{
if (!first && verse.IsChapter())
{
yield return verses;
verses.Clear();
}
verses.Add(verse);
first = false;
}
yield return verses;
}
public IEnumerable<IEnumerable<Row>> Chapters(IEnumerable<Row> book)
{
bool first = true;
List<Row> verses = new List<Row>();
foreach (Row verse in book)
{
if (!first && verse.IsChapter())
{
yield return verses;
verses.Clear();
}
verses.Add(verse);
first = false;
}
yield return verses;
}
Servator
Servator2y ago
.GroupBy() maybe ?
BenMcLean
BenMcLeanOP2y ago
How would that work? I edited the code I think GroupBy would work if I had the name of each chapter or a chapter number in a column of my data, but not if I just have a "1" for the start of a new chapter
Servator
Servator2y ago
let me write an example
Saber
Saber2y ago
doubt groupby would work here
MODiX
MODiX2y ago
Samarichitane#3472
REPL Result: Success
var collection = Enumerable.Range(1, 10);

var groups = collection.GroupBy(i => i % 2 == 0).ToList();

var evens = groups.First(ints => ints.Key);
var odds = groups.First(ints => !ints.Key);

Console.WriteLine(string.Join(", ", evens));
Console.WriteLine(string.Join(", ", odds));
var collection = Enumerable.Range(1, 10);

var groups = collection.GroupBy(i => i % 2 == 0).ToList();

var evens = groups.First(ints => ints.Key);
var odds = groups.First(ints => !ints.Key);

Console.WriteLine(string.Join(", ", evens));
Console.WriteLine(string.Join(", ", odds));
Console Output
2, 4, 6, 8, 10
1, 3, 5, 7, 9
2, 4, 6, 8, 10
1, 3, 5, 7, 9
Compile: 605.878ms | Execution: 106.237ms | React with ❌ to remove this embed.
Saber
Saber2y ago
lol
Servator
Servator2y ago
Just pass chapter number there you go
BenMcLean
BenMcLeanOP2y ago
But my source data doesn't have chapter number, hence the question Yeah if I had the chapter number in a column, that would make this real easy I didn't make this and I'm trying to convert this data into something that's nicer for computers to read for the future.
Servator
Servator2y ago
Still you can pass as a value tuple (IsABook, IsAChapter)
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server