score:1
your example is complicated. i'll first state and solve a simpler problem, then use the same method to solve your original problem.
i want to split a list of numbers into contiguous groups of even and odd numbers. for example, given the list 2,2,4,3,6,2
i would split it into three groups [2,2,4], [3], [6,2]
this can be done concisely with a groupadjacentby method
> var numbers = new list<int>{2,2,4,3,6,2};
> numbers.groupadjacentby(x => x % 2)
[[2,2,4], [3], [6,2]]
to solve your problem, simply replace the even-odd classifying function above with your classification function:
> var points = new list<int>{-180,180};
> var f = new func<int,int>(x => points.binarysearch(x));
> var numbers = new list<int>{6,-50,100,190,200,20};
> numbers.groupadjacentby(f)
[[6,-50,100], [190,200], [20]]
score:0
if you need the collections to be updated as soon as the values change why don;t you use properties? something like
// your original collection
public ilist<double> originalvalues; //= new list<double> { -1000, 5, 7 1000 };
public ilist<double> belowminus180
{
get { return originalvalues.where(x => x < -180).tolist().asreadonly(); }
}
public ilist<double> betweenminus180and180
{
get { return originalvalues.where(x => x >= -180 && x <= 180).tolist().asreadonly(); }
}
public ilist<double> above180
{
get { return originalvalues.where(x => x > 180).tolist().asreadonly(); }
}
score:0
public static list<list<t>> partitionby<t>(this ienumerable<t> seq, func<t, bool> predicate)
{
bool lastpass = true;
return seq.aggregate(new list<list<t>>(), (partitions, item) =>
{
bool inc = predicate(item);
if (inc == lastpass)
{
if (partitions.count == 0)
{
partitions.add(new list<t>());
}
partitions.last().add(item);
}
else
{
partitions.add(new list<t> { item });
}
lastpass = inc;
return partitions;
});
}
you can then use:
list<list<double>> segments = newdataset.partitionby(d => d > -180 && d < 180);
score:0
how about this possible solution using two passes. in the first pass we find the indices were a change occurs, and in the second pass we do the actual partitioning. first an auxiliary method to determine the category:
protected int determinecategory(double number)
{
if (number < 180 && number > -180)
return 0;
else if (number < -180)
return 1;
else
return 2;
}
and then the actual algorithm:
list<int> indices = new list<int>();
int currentcategory = -1;
for (int i = 0; i < numbers.count; i++)
{
int newcat = determinecategory(numbers[i]);
if (newcat != currentcategory)
{
indices.add(i);
currentcategory = newcat;
}
}
list<list<double>> collections = new list<list<double>>(indices.count);
for (int i = 1; i < indices.count; ++i)
collections.add(new list<double>(
numbers.skip(indices[i - 1]).take(indices[i] - indices[i - 1])));
score:0
here is a new answer based on the new info you provided. i hope this time i will be closer to what you need
public ienumerable<ilist<double>> getcollectionofcollections(ilist<double> values, ilist<double> boundries)
{
var ordered = values.orderby(x => x).tolist();
for (int i = 0; i < boundries.count; i++)
{
var collection = ordered.where(x => x < boundries[i]).tolist();
if (collection.count > 0)
{
ordered = ordered.except(collection).tolist();
yield return collection.tolist();
}
}
if (ordered.count() > 0)
{
yield return ordered;
}
}
score:0
one method with linq. untested but should work
var firstset = dataset.takewhile(x=>x>-180&&x<180);
var totalcount = firstset.count();
var secondset = dataset.skip(totalcount).takewhile(x=>x<-180);
totalcount+=secondset.count();
var thirdset = dataset.skip(totalcount).takewhile(x=>x>180);
totalcount += thirdset.count();
var fourthset = dataset.skip(totalcount);
Source: stackoverflow.com
Related Query
- Split a collection of double by size of its contents
- Split a collection into `n` parts with LINQ?
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- LINQ WHERE method alters source collection
- Using LINQ to generate a random size collection filled with random numbers
- How to order a collection and its subcollection using LINQ?
- Split a collection into parts based on condition with LINQ?
- Split collection into objects based on condition and occurrence
- LINQ Source Code Available
- Split a collection into n parts with LINQ, in VB.Net
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- .NET 4 Code Contracts: "requires unproven: source != null"
- Unit testing collection contents meet certain criteria using lambdas/LINQ
- How to get list for a property of a class from its collection
- Filtering a collection of items from contents of another collection
- Get differencing items between a collection and its subgroup
- creating Linq to sqlite dbml from DbLinq source code
- How to return first object of a collection from its parent
- Code Cleanup: Best way to split long statement over multiple lines
- Split a List of structs into sublists based on the stucts contents
- Split string in Entity framework in Object Collection i.e in New {}
- Understanding Deferred Execution: Is a Linq Query Re-executed Everytime its collection of anonymous objects is referred to?
- Group a C# collection by inequality with a maximum group size using LINQ
- Split a entity collection into n parts
- How to modify an item in an IEnumerable collection and retain its order
- EF Code first - add collection to a collection, appropriate usage of skip() and take()
- C# - how to split a string and filter some of its entries
- LINQ operator to split List of doubles to multiple list of double based on generic delimiter
- Split collection into n of parts is not giving the desired resulting secuences
- Combine contents of list member of object collection into single list with linq
More Query from same tag
- Distinct() not working, behaving differently in .NET 5 and 4.7.2
- Use DataContractSerializer to Deserialize only part of an XML document
- Assignment in .NET 3.5 expression trees
- Query UUID Key using Linq + MongoDb Driver
- Using the GLOB function in LINQ to Entities
- System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda throwing System.NotSupportedException
- [LINQ]InsertOnSubmit NullReferenceException
- LINQ on the .NET 2.0 Runtime
- Convert DateTime format to MMMM yyyy, dd using lambda expression
- VB.NET & Linq: Sum datatable column and Group by date in another column
- Retrieving values from a Linq GroupBy
- Why is this LINQ query not working correctly?
- Linq join two List<T> using multiples flags
- How to find a holder of a property
- cannot resolve method Sum
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- How to get items that were effective within a date range in linq
- Why doesn't LINQ's First method simply return null if no item is found?
- Entity Framework getting attribute/value pairs
- Return filtered collection of results
- query that gets only the records needed to the page using Linq- not fetched the record based on page size
- Array.Count() much slower than List.Count()
- How to get proper data using LINQ Lambda expression with many to many relation
- How to search in a List<classtype> by dynamic field name of class in c#
- How I get id and total value of dynamically created text boxes?
- How can I make sure my LINQ queries execute when called in my DAL, not in a delayed fashion?
- Can I make this two LINQ queries into one query only?
- How to get selected checkboxes in Grid using lambda expression
- Converting a string into Linq lambda expression
- Do you choose Linq over Forloops?