score:1
Accepted answer
refinedresults = initialresults
.where(x => {
var m = regex.match(x, @"^(\d+)-(\d+)");
return m.success
&& int.parse(m.groups[1]) >= data.timerange[0]
&& int.parse(m.groups[2]) <= data.timerange[1];
}).toarray();
score:0
initialresults
.select(x => new {
timelevel = x.timelevel,
minmaxpart = x.timelevel.split(' ')[0]
})
.select(x => new {
timelevel = x.timelevel,
min = int.parse(x.minmaxpart.split('-')[0].trim('+')),
max = int.parse(x.minmaxpart.split('-')[1].trim('+'))
})
.where(x => x.min >= data.timerange[0] && x.max <= data.timerange[1])
.select(x => x.timelevel)
.toarray();
score:1
using system;
using system.linq;
using system.text.regularexpressions;
namespace consoleapplication2
{
public class sampletime
{
public sampletime(string timelevel)
{
timelevel = timelevel;
}
public readonly string timelevel;
}
public class data
{
public int[] timerange = new int[2];
}
class program
{
private static void main(string[] args)
{
var initialresults = new []
{
new sampletime("0-5 minutes"),
new sampletime("4-5 minutes"), // should be selected below.
new sampletime("1-8 minutes"),
new sampletime("4-6 minutes"), // should be selected below.
new sampletime("4-7 minutes"),
new sampletime("5-6 minutes"), // should be selected below.
new sampletime("20-30 minutes")
};
// find all ranges between 4 and 6 inclusive.
data data = new data();
data.timerange[0] = 4;
data.timerange[1] = 6;
// the output of this should be (as commented in the array initialisation above):
//
// 4-5 minutes
// 4-6 minutes
// 5-6 minutes
// here's the significant code:
var refinedresults =
(
from result in initialresults
let numbers = regex.matches(result.timelevel, @"\d+")
where ((int.parse(numbers[0].value) >= data.timerange[0]) && (int.parse(numbers[1].value) <= data.timerange[1]))
select result
).toarray();
foreach (var result in refinedresults)
{
console.writeline(result.timelevel);
}
}
}
}
Source: stackoverflow.com
Related Query
- Input string was not in the correct format C# ASP.NET
- Input string was not in a correct format
- Linq Input string was not in a correct format in MVC
- Input string was not in a correct format error, but still able to get parsed value from LINQ to XML query
- Input string was not in a correct format in Entity Framework
- Input string was not in correct format error in LINQ to Excel query
- Linq Error Input string was not in a correct format. System.Exception {System.FormatException}
- Input string for parsing CSV file not in correct format
- Int.Parse(String.Split()) returns "Input string was not in a correct format" error
- "Input string was not in a correct format" when using LINQ
- Linq error "Input string was not in a correct format."
- C# Linq .Select(int.parse) on empty array results in "Input string was not in a correct format"
- uint.Parse on a valid string throws System.FormatException: 'Input string was not in a correct format.'
- exception( Input string wasn't in the correct format )
- Select parsed int, if string was parseable to int
- Linq to SQL .Equals returns true when comparing int to string --> Id.Equals("5") returns true
- Split string and set default value if separator not found WITH LINQ
- linq to entities can not convert int to string
- "Specified cast is not valid" when casting array of string to array of int
- LINQ to Entities does not recognize the method 'Int32 ToInt32 Converting String to Int
- System.FormatException: String was not recognized as a valid DateTime
- String was not recognized as a valid datetime in the where statement
- How to tell which delimiter string was split on
- How to replace invalid datetime string with a correct format of datetime string when using DateTime.ParseExact()?
- date format not correct when pulling from database using linq query
- String was not recognized as a valid DateTime - Whats wrong?
- Sort the list of strings after string data was added to the list and add method returns void
- String was not recognized as a valid DateTime, stored procedure empty parameter
- LINQ ToList() Method throws FormatException: String was not recognized as a valid DateTime
- Convert string to int array using LINQ
More Query from same tag
- Why does Enumerable<T>.ToArray() use an intermediary Buffer when it can just call Count() first?
- Grouping by anonymous v.s. grouping by non-anonymous
- Natural sort filenames with leading numbers in List(Of IO.FileInfo)?
- Render html control while using with lambda expression
- Updating data in complex XML
- Avoiding code repetition when using LINQ
- LINQ: take a sequence of elements from a collection
- Examples of using Linq to XML in c# for parsing XML
- IF ELSE condition in SELECT statement of LINQ query with DATEADD() logic
- Comparing 2 lists with different references (C#)
- Filter EntityDataSource for DateTime field
- C# Splitting a List<string> Value
- Map Linq groupby count to C# entity
- Simple update with Entity Framework
- .NET C# I want to get the IDs (1/multiple) from a data if it much the ID of another table
- linq to entities changing database connection string in code
- C# linq XML DeepCompare and tag removal
- Making this LINQ projection query better
- EF5: LINQ to Entities does not recognize the method System.Nullable System.Decimal Median
- Query for List within List
- Is it possible to reuse parts of a LINQ to Entities select statement?
- How to populate an AutoCompleteBox filter with distinct values from one property?
- What can I do with the IEnumerable(of T) result except iterate over it?
- Setting alternative records true if its even count else leave last record
- Remove parent items from a list when having chilid items by checking parent IDs of the list c#
- Linq ForEach - Returning cannot assign 'void' to an implicitly-typed local variable
- Linq issues querying two Collections of data
- LinqKit Unable to cast object of type 'System.Linq.Expressions.InstanceMethodCallExpression2' to type 'System.Linq.Expressions.LambdaExpression'
- Specified cast is not valid when using casting
- Whats the best solution to Entity Framework cores lack of moderate LINQ query support?