score:0
your solution is okay. i don't think that linq adds any simplification or clarity in this situation. here is a fast solution that i find intuitive:
static list<item[]> getcontiguoussequences(item[] items)
{
var result = new list<item[]>();
int start = 0;
while (start < items.length) {
int end = start + 1;
while (end < items.length && items[end].start == items[end - 1].stop) {
end++;
}
int len = end - start;
var a = new item[len];
array.copy(items, start, a, 0, len);
result.add(a);
start = end;
}
return result;
}
score:1
i'm really not sure this is much better than your original, but for the purpose of another solution the general process is
- use
select
to project a list working out a grouping - use
groupby
to group by the above - use
select
again to project the grouped items to an array ofitem
- use
tolist
to project the result to a list
public static list<item[]> getcontiguoussequences2(item []items)
{
var curridx = 1;
return items.select( (item,index) => new {
item = item,
index = index == 0 || items[index-1].stop == item.start ? curridx : ++curridx
})
.groupby(x => x.index, x => x.item)
.select(x => x.toarray())
.tolist();
}
live example: https://dotnetfiddle.net/mbfhru
another way is to do an aggregation using aggregate
. this means maintaining a final result
list and a curr
list where you can aggregate your sequences, adding them to the result
list as you find discontinuities. this method looks a little closer to your original
public static list<item[]> getcontiguoussequences3(item []items)
{
var res = items.aggregate(new {result = new list<item[]>(), curr = new list<item>()}, (agg, item) => {
if(!agg.curr.any() || agg.curr.last().stop == item.start) {
agg.curr.add(item);
} else {
agg.result.add(agg.curr.toarray());
agg.curr.clear();
agg.curr.add(item);
}
return agg;
});
res.result.add(res.curr.toarray()); // remember to add the last group
return res.result;
}
live example: https://dotnetfiddle.net/hl0vyj
score:1
you can implement contiguoussplit
as a corutine: let's loop over source
and either add item
into current
range or return it and start a new one.
private static ienumerable<item[]> contiguoussplit(ienumerable<item> source) {
list<item> current = new list<item>();
foreach (var item in source) {
if (current.count > 0 && current[current.count - 1].stop != item.start) {
yield return current.toarray();
current.clear();
}
current.add(item);
}
if (current.count > 0)
yield return current.toarray();
}
then if you want materialization
list<item[]> getcontiguoussequences(item []items) => contiguoussplit(items).tolist();
Source: stackoverflow.com
Related Query
- Split a list of objects into sub-lists of contiguous elements using LINQ?
- Using linQ to group list of object into new lists of objects
- Creating sub lists with objects of the same attribute from a list using LINQ
- Using Linq to group a list of objects into a new grouped list of list of objects
- Split string into list of N-length strings using LINQ
- LINQ query to split an ordered list into sublists of contiguous points by some criteria
- Linq - merging sub lists from different objects into a single object
- Using Linq to group a list of objects that contains primitives data into a new grouped list of objects
- LINQ Split list into lists by date
- Get the count of distinct elements from a list of lists using LINQ
- Converting a list of lists into a single list using linq
- Split list into two lists with single LINQ statement
- Using LINQ how to transform a List of Lists using elements index
- how split the List of strings into another Lists if particular string matches condition using linq?
- List of EF objects to Dictionary of lists using linq
- Reduce a list into smaller lists of two using LINQ (Functional Programming)
- Grouping a list of objects by parent using linq to output child elements
- Split list into sets using Linq
- How to split multi-level list of tuples into multi-level lists of values using LINQ?
- Split List into Sublist using Linq based on content of list
- Aggregating Lists inside of a List of objects using LINQ
- Split a list into multiple lists using lambda expression
- Split array or list into segments using LINQ
- Construct a list of wpf Hyperlink elements from an XML source file using Linq
- Using Linq to do: Compile a list of all lists within a list of objects
- Using Linq to group a list of objects into a new grouped list of list of objects with 4 objects in each group
- Remove set of elements from list A and add into list B using Linq
- Return select objects and only desired subvalues from a list of lists using LINQ
- Merging lists of objects using linq while showing preference over a condition in one list
- Using linq to instantiate objects containing lists of other objects, where query criteria is on inner list
More Query from same tag
- find all id where deleted column value should be 0
- One to many linq query
- What is meant by 'The specified LINQ expression contains references to queries that are associated with different contexts'
- LINQ to SQL Reflection in Select
- Getting 'Data source is an invalid type' when binding Linq query to Gridview
- LINQ not updating on .SubmitChanges()
- Translating Linq expression in c#
- Creating a one to many relationship
- How to add some number to XML element's value (it's concatenating instead of adding)?
- is there another way to do this query? (contains a split string in linq-to-sql query)
- Select from one list only those present in another and order by second list - linq
- XML Node Access
- Mongodb C# Driver Unsupported filter error with specific linq predicate
- List of class properties
- Combine Two Entities Into One Using LINQ
- Xamarin Android app crash in Release mode (Parse.Android SDK)
- Paging using include in Linq to Entities does not work
- Pagination in C# against DocumentDB without Skip
- How to select Columns using where condition from one DataTable to another in C#
- How to create MongoDB MultiKey index on attribute of items in an array .NET Driver
- Sorting jqGrid in ASP.NET MVC client view with jQuery and LINQ-to-Entities
- linq to entities, a where in where clause? (inner where)
- Cast troubles while using LINQ to filter a Dictionary by values
- Getting the MAX of several SUMs with LINQ
- Load a single related object
- Is Linq-To-SQL getting scrapped?
- C# Linq to VB.NET (not a From In Where ... query style)
- Linq Error with Count(column)
- Saving image to database as varbinary, arraylength (part 2)
- Outputting linq getting overflow exception