void Main() { var list=new List<model> { new model{ a=1, b=1.1F,c=1.8F}, new model{ a=2, b=2.2F,c=1.4F}, new model{ a=3, b=3.1F,c=1.6F}, new model{ a=5, b=4.1F,c=1.5F}, new model{ a=7, b=5.1F,c=1.3F}, new model{ a=9, b=7.1F,c=1.7F}, new model{ a=4, b=8.1F,c=1.9F}, new model{ a=11, b=11.1F,c=1.66F}, new model{ a=6, b=12.1F,c=1.22F} }; float[] arr=new float[]{1F,1.2F,1.3F,1.4F,1.5F,1.6F,1.7F,2.2F,3.3F,4.4F,5.5F,6.6F,7.7F}; var query=from a in arr.Select((a,b)=>new{index=b,value=a}) join l in list on a.index equals l.a where a.value>=l.b && a.value<l.c select l; Console.WriteLine("a\tb\tc"); query.ToList().ForEach(q=>Console.WriteLine("{0}\t{1}\t{2}",q.a,q.b,q.c)); /* a b c 1 1.1 1.8 */ } public class model { public int a{get;set;} public float b{get;set;} public float c{get;set;} }
var s = from m in list where (arr[m.a] >= m.b) && (arr[m.a] <= m.c) select m;
@gomoku @lihanbing 你们的方法都可以 不过 我的对@gomoku大大的 var query=from a in arr.Select((a,b)=>new{index=b,value=a}) join l in list on a.index equals l.a where a.value>=l.b && a.value<l.c select l; 写法很感兴趣 能解释下吗?
Select - Indexed
This sample uses an indexed Select clause to determine if the value of ints in an array match their position in the array.
void Main()
{
var list=new List<model>
{
new model{ a=1, b=1.1F,c=1.8F},
new model{ a=2, b=2.2F,c=1.4F},
new model{ a=3, b=3.1F,c=1.6F},
new model{ a=5, b=4.1F,c=1.5F},
new model{ a=7, b=5.1F,c=1.3F},
new model{ a=9, b=7.1F,c=1.7F},
new model{ a=4, b=8.1F,c=1.9F},
new model{ a=11, b=11.1F,c=1.66F},
new model{ a=6, b=12.1F,c=1.22F}
};
float[] arr=new float[]{1F,1.2F,1.3F,1.4F,1.5F,1.6F,1.7F,2.2F,3.3F,4.4F,5.5F,6.6F,7.7F};
var query=from a in arr.Select((a,b)=>new{index=b,value=a})
join l in list
on a.index equals l.a
where a.value>=l.b && a.value<l.c
select l;
Console.WriteLine("a\tb\tc");
query.ToList().ForEach(q=>Console.WriteLine("{0}\t{1}\t{2}",q.a,q.b,q.c));
/*
a b c
1 1.1 1.8
*/
}
public class model
{
public int a{get;set;}
public float b{get;set;}
public float c{get;set;}
}
var selected = list.Where(l => l.a < arr.Length && arr[l.a] >= l.b && arr[l.a] < l.c);
var s = from m in list
where (arr[m.a] >= m.b) && (arr[m.a] <= m.c)
select m;
@lihanbing
你们的方法都可以 不过 我的对@gomoku大大的
var query=from a in arr.Select((a,b)=>new{index=b,value=a})
join l in list
on a.index equals l.a
where a.value>=l.b && a.value<l.c
select l;
写法很感兴趣 能解释下吗?
This sample uses an indexed Select clause to determine if the value of ints in an array match their position in the array.
(back to top)
复制代码 C#
public void Linq12()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) });
Console.WriteLine("Number: In-place?");
foreach (var n in numsInPlace)
{
Console.WriteLine("{0}: {1}", n.Num, n.InPlace);
}
}
Result
Number: In-place?
5: False
4: False
1: False
3: True
9: False
8: False
6: True
7: True
2: False
0: Falsehttp://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b