集合中的c#范围查找
我已在StackOverflow中搜索,但找不到我的问题 我有一些数据如下:集合中的c#范围查找,c#,collections,C#,Collections,我已在StackOverflow中搜索,但找不到我的问题 我有一些数据如下: 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和b”的列表,如果(17,20)通过,我想要一个“b和
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和b”的列表,如果(17,20)通过,我想要一个“b和c”的列表,如果(3250)通过,那么我想要一个所有的列表
我不知道应该将值(3、13、对象a)和其他值存储在哪种类型的集合中
你能给这个集合命名并举个例子吗
先谢谢你
p、 美国。如果因为我的英语而无法很好地描述,很抱歉,谢谢大家。因此,您希望找到最小值小于/等于传递的最小值,最大值大于/等于传递的最大值的对象
var query = objects.Where(obj=> obj.MinVal <= minVal && obj.MaxVal >= maxVal);
var query=objects.Where(obj=>obj.MinVal=maxVal);
你能给这个集合命名并举个例子吗
所以你没有收藏?您应该填写一个
列表
,其中范围
是一个自定义类,至少有两个属性MinVal
和MaxVal
,您可以创建自己的类型,也可以使用元组
表示一个对象。然后,您可以创建并填充这些对象的列表
,以存储整个集合。之后,您可以使用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);
List YourCollection=new List();
添加(新元组(3,13));
添加(新元组(11,20));
添加(新元组(16,21));
var Results=YourCollection.Where(x=>x.Item1void Main()
{
var输入=新列表
{
新间隔{Name=“a”,最小值=3,最大值=13},
新间隔{Name=“b”,最小值=11,最大值=20},
新间隔{Name=“c”,最小值=16,最大值=21},
新间隔{Name=“z”,最小值=200,最大值=250}
};
var interval=newinterval{Name=“search”,Min=12,Max=13};
//当间隔时间结束时,不要忘记第三种情况
//您要查找的是输入间隔内的
//最小值=210,最大值=220应返回“z”
var result=input.Where(i=>(interval.Min使用一个@aush代码分支如果您可以在类System.Collections.CollectionBase中嵌入该类,则会更好,然后您可以轻松地实现该类
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;
}
}
我不完全确定你问的是什么,但你应该看看字典、KeyValuePair或Tuple
。我不知道这个问题与字典有什么关系。你需要更详细地说明你的要求。大多数人认为你想要的范围完全包括给定的范围,但我不知道我相信你真的想要与给定范围相交的任何范围。如果你想要在集合中搜索范围,那么下面@TimSchmelter的答案可以满足你的需要,但我不确定这就是你想要的。如果你想要多个值等同于一个字典的键,那么你必须编写自己的数据结构来实现这一点。你的情况太复杂了。不是吗?@dotNET,不,这里有三种情况。@dotNET语句“(3250)那么我想要一个所有的列表。”表明他们想要相交的范围,而不是包含给定范围的范围。@aush:你的情况可以改进。检查我的答案。OP声明“(3250)所以我相信他们想要的是与给定范围相交的范围,而不是包含给定范围的范围。
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;
}
}
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);