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 Articles
- 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
- 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
- Filtering a collection of items from contents of another collection
- creating Linq to sqlite dbml from DbLinq source code
- 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 {}
- Group a C# collection by inequality with a maximum group size using LINQ
- Split a entity collection into n parts
- EF Code first - add collection to a collection, appropriate usage of skip() and take()
- 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
- source code for LINQ 101 samples
- How to split an array of objects into multiple sub arrays of a given size
- Linq: Split list by condition and max size of n
- Split collection into groups of different sizes
- Split collection linq entity framework
- Linq: Split contents of a string column and add it to the array returned by Select(o=>o.ColumnName)
- Split collection into multiple collections
- Comparing two lists and removing each item that contains an entry from the other list
- LINQ: Dealing with anonymous types
- InvalidCastException in a LINQ query
- Why can't I query Microsoft.Office.Interop.Excel.Connections?
- Convert DataTable into object[ , ] array with LINQ
- Linq, using ToLookup to project values to different named variables
- Using DateTime in Dynamic LINQ to Entities
- "The specified type member 'Date' is not supported in LINQ "
- LINQ Join returning IQueryable
- How to join every row from one table, with the latest dated row from another table, using LINQ2SQL
- Linq to ADO.NET parent/child query help
- How to convert a data table to a list of strongly typed objects in C# using LINQ?
- How can I use LINQ to select a property and also other items of a list at the same level?
- .Select() in LINQ
- EF Gather all child records into list with a parent member
- Observing incoming websocket messages with Reactive Extensions?
- Query to get information from multiple tables
- How to count occurence of partly-matching words with VB.NET?
- Query a subset of an entity and cast it into another model using EF 6
- LINQ to pull back object graph