集合中的 c# 范围查找
c# range lookup within a collection
我在 Whosebug 中进行了搜索,但找不到我的问题。
我有一些这样的数据:
object a -> min:3 max:13
object b -> min:11 max:20
object c -> min:16 max:21
...
z-> min:200 max:250
对于指定的时间间隔,我期望 a、b、c 或其他对象或列表。
例如,如果 (6,8) 被传递,那么我想要 "a",如果 (12,13) 被传递,我想要一个 "a and b" 的列表,如果 (17 , 20) 已通过我想要一个 "b and c" 的列表,如果 (3,250) 那么我想要一个所有的列表。
我不知道应该将值(3、13、对象 a)和其他值存储在哪种类型的集合中。
能否为该集合命名并举例说明?
提前致谢...
p.s。对不起,如果我因为我的英语不能很好地描述,谢谢大家
所以你想找到 objects,其中最小值是 smaller/equal 传递的 min-value,最大值是 larger/equal 传递的 max-value。
var query = objects.Where(obj=> obj.MinVal <= minVal && obj.MaxVal >= maxVal);
Can you name the collection and give an example?
所以你没有collection?您应该填写 List<Range>
,其中 Range
是自定义 class,至少有两个属性 MinVal
和 MaxVal
.
您可以创建自己的类型,也可以使用 Tuple<int, int>
来表示一个对象。然后您可以创建并填充这些对象的 List
来存储您的整个集合。之后您可以使用 LINQ 查询所需的对象:
List<Tuple<int, int>> YourCollection = new List<Tuple<int, int>>();
YourCollection.Add(new Tuple<int, int>(3, 13));
YourCollection.Add(new Tuple<int, int>(11, 20));
YourCollection.Add(new Tuple<int, int>(16, 21));
var Results = YourCollection.Where(x => x.Item1 <= MAX && MIN <= x.Item2);
其中 MIN 和 MAX 定义了您感兴趣的范围。请注意,上面的条件寻找重叠(交叉)似乎是需要的。
void Main()
{
var input = new List<Interval>
{
new Interval { Name = "a", Min = 3, Max = 13 },
new Interval { Name = "b", Min = 11, Max = 20 },
new Interval { Name = "c", Min = 16, Max = 21 },
new Interval { Name = "z", Min = 200, Max = 250 }
};
var interval = new Interval { Name = "search", Min = 12, Max = 13 };
// Don't forget the third case when the interval
// you're looking for is inside your input intervals
// Min = 210, Max = 220 should return "z"
var result = input.Where(i => (interval.Min <= i.Min && i.Min <= interval.Max) ||
(interval.Min <= i.Max && i.Max <= interval.Max) ||
(i.Min <= interval.Min && interval.Max <= i.Max));
}
class Interval
{
public string Name;
public int Min;
public int Max;
}
使用@aush 代码的一个分支,如果你可以继承 class System.Collections.CollectionBase 就更好了,然后你可以轻松实现这个 class.
structure Interval{
public string Name;
public int Min;
public int Max;
public Interval(string Name,int Min,int Max){
this.Name = Name;
this.Min = Min;
this.Max = Max;
}
public bool IsInsideOfRange(int value){
if(value >= this.Min && value <= this.Max){
return true;
}else{
return false;
}
}
public overrides ToString(){
return this.Name;
}
}
class IntervalCollection : System.Collections.CollectionBase {
public void Add(string Name,int Min,int Max){
Interval Item = new Interval(Name,Min,Max);
this.List.Add(Item);
}
public void Add(Interval Item){
this.List.Add(Item);
}
public string Encode(param int[] values){
string EcodedText = "";
foreach(int iValue in values){
foreach(Interval Item in this){
if(Item.IsInsideOfRange(iValue)){
EncodedText +=Item.ToString();
}
}
}
return EcodedText;
}
}
你可以这样实现class
IntervalCollection Intervals = new IntervalCollection();
string EncodeText = "";
Intervals.Add(new Interval { Name = "a", Min = 3, Max = 13 });
Intervals.Add(new Interval { Name = "b", Min = 11, Max = 20 });
Intervals.Add(new Interval { Name = "c", Min = 16, Max = 21 });
Intervals.Add( "z", 200, 250 }); //you can add item in this way too.
EncodeText = Intervals.Encode(6,8,12,13,17,20);
我在 Whosebug 中进行了搜索,但找不到我的问题。
我有一些这样的数据:
object a -> min:3 max:13
object b -> min:11 max:20
object c -> min:16 max:21
...
z-> min:200 max:250
对于指定的时间间隔,我期望 a、b、c 或其他对象或列表。
例如,如果 (6,8) 被传递,那么我想要 "a",如果 (12,13) 被传递,我想要一个 "a and b" 的列表,如果 (17 , 20) 已通过我想要一个 "b and c" 的列表,如果 (3,250) 那么我想要一个所有的列表。
我不知道应该将值(3、13、对象 a)和其他值存储在哪种类型的集合中。
能否为该集合命名并举例说明?
提前致谢...
p.s。对不起,如果我因为我的英语不能很好地描述,谢谢大家
所以你想找到 objects,其中最小值是 smaller/equal 传递的 min-value,最大值是 larger/equal 传递的 max-value。
var query = objects.Where(obj=> obj.MinVal <= minVal && obj.MaxVal >= maxVal);
Can you name the collection and give an example?
所以你没有collection?您应该填写 List<Range>
,其中 Range
是自定义 class,至少有两个属性 MinVal
和 MaxVal
.
您可以创建自己的类型,也可以使用 Tuple<int, int>
来表示一个对象。然后您可以创建并填充这些对象的 List
来存储您的整个集合。之后您可以使用 LINQ 查询所需的对象:
List<Tuple<int, int>> YourCollection = new List<Tuple<int, int>>();
YourCollection.Add(new Tuple<int, int>(3, 13));
YourCollection.Add(new Tuple<int, int>(11, 20));
YourCollection.Add(new Tuple<int, int>(16, 21));
var Results = YourCollection.Where(x => x.Item1 <= MAX && MIN <= x.Item2);
其中 MIN 和 MAX 定义了您感兴趣的范围。请注意,上面的条件寻找重叠(交叉)似乎是需要的。
void Main()
{
var input = new List<Interval>
{
new Interval { Name = "a", Min = 3, Max = 13 },
new Interval { Name = "b", Min = 11, Max = 20 },
new Interval { Name = "c", Min = 16, Max = 21 },
new Interval { Name = "z", Min = 200, Max = 250 }
};
var interval = new Interval { Name = "search", Min = 12, Max = 13 };
// Don't forget the third case when the interval
// you're looking for is inside your input intervals
// Min = 210, Max = 220 should return "z"
var result = input.Where(i => (interval.Min <= i.Min && i.Min <= interval.Max) ||
(interval.Min <= i.Max && i.Max <= interval.Max) ||
(i.Min <= interval.Min && interval.Max <= i.Max));
}
class Interval
{
public string Name;
public int Min;
public int Max;
}
使用@aush 代码的一个分支,如果你可以继承 class System.Collections.CollectionBase 就更好了,然后你可以轻松实现这个 class.
structure Interval{
public string Name;
public int Min;
public int Max;
public Interval(string Name,int Min,int Max){
this.Name = Name;
this.Min = Min;
this.Max = Max;
}
public bool IsInsideOfRange(int value){
if(value >= this.Min && value <= this.Max){
return true;
}else{
return false;
}
}
public overrides ToString(){
return this.Name;
}
}
class IntervalCollection : System.Collections.CollectionBase {
public void Add(string Name,int Min,int Max){
Interval Item = new Interval(Name,Min,Max);
this.List.Add(Item);
}
public void Add(Interval Item){
this.List.Add(Item);
}
public string Encode(param int[] values){
string EcodedText = "";
foreach(int iValue in values){
foreach(Interval Item in this){
if(Item.IsInsideOfRange(iValue)){
EncodedText +=Item.ToString();
}
}
}
return EcodedText;
}
}
你可以这样实现class
IntervalCollection Intervals = new IntervalCollection();
string EncodeText = "";
Intervals.Add(new Interval { Name = "a", Min = 3, Max = 13 });
Intervals.Add(new Interval { Name = "b", Min = 11, Max = 20 });
Intervals.Add(new Interval { Name = "c", Min = 16, Max = 21 });
Intervals.Add( "z", 200, 250 }); //you can add item in this way too.
EncodeText = Intervals.Encode(6,8,12,13,17,20);