# 请教List求交集的Linq问题

2

`class Program{    static void Main(string[] args)    {        List<A> aList = new List<A>()        {            new A(){ Id=1,Names=new List<string>(){"a","b","c"}},            new A(){ Id=2,Names=new List<string>(){"b","c","d"}},            new A(){ Id=3,Names=new List<string>(){"c","d","e"}},            new A(){ Id=4,Names=new List<string>(){"d","e","f"}},            new A(){ Id=5,Names=new List<string>(){"e","f","g"}}        };        List<string> test = new List<string>() { "b", "f" };        var val=//求aList中Names属性与test有交集的A集合        Console.Read();    }}public class A{    public int Id { get; set; }    public List<string> Names { get; set; }}`

4
`var val = aList.FindAll(a => a.Names.Any(test.Contains));`

LCM | 大侠五级 |园豆：6876 | 2012-02-13 11:26

@水牛刀刀: 多谢提醒。那把FindAll换成where不就可以了。

`var val = aList.Where(a => a.Names.Any(test.Contains));`

LCM | 园豆：6876 (大侠五级) | 2012-02-13 13:51

@LCM: :)

@水牛刀刀: 不过我测了一下这三种的性能，你看下代码。

`Stopwatch stw = new Stopwatch();            stw.Start();            for (int i = 0; i < 1000; i++)            {                var val = aList.FindAll(a => a.Names.Any(test.Contains));            }            stw.Stop();            Console.WriteLine(stw.ElapsedTicks);            //---------------------------------            stw.Reset();            stw.Start();            for (int i = 0; i < 1000; i++)            {                var val1 = aList.Where(a => a.Names.Any(test.Contains));            }            stw.Stop();            Console.WriteLine(stw.ElapsedTicks);            //---------------------------------            stw.Reset();            stw.Start();            for (int i = 0; i < 1000; i++)            {            var val2 = aList.Where(a => a.Names.Intersect(test, StringComparer.OrdinalIgnoreCase).Any()).ToList();            }            stw.Stop();            Console.WriteLine(stw.ElapsedTicks);`

5539254
537250
15661632

LCM | 园豆：6876 (大侠五级) | 2012-02-13 13:59

@LCM: 第二种，你ToList一下，应该跟第一种差不多的。第三种多遍历了数据，所以会最慢。

@水牛刀刀: 嗯

LCM | 园豆：6876 (大侠五级) | 2012-02-13 14:32

@LCM:

@乱世文章:

`var val1 = aList.Where(a => a.Names.Any(n => test.Any(t => t.Equals(n, StringComparison.InvariantCultureIgnoreCase)));`

@水牛刀刀: 正解

LCM | 园豆：6876 (大侠五级) | 2012-02-13 17:03

1
`var val = aList.Where(a => a.Names.Intersect(test, StringComparer.OrdinalIgnoreCase).Any()).ToList();`

artwl | 园豆：16526 (专家六级) | 2012-02-13 13:42

1
```public class newArray<K> implements List<K>{

private Object[] a =  new Object[8];
private int at = 0;

private void setSize(Object[] a,int index){
if( index > a.length-1 ){
int times = (int) Math.ceil((float)index/(float)(a.length-1));
int length = (int)times *(a.length);
Object[] b = new Object[length];
for(int i = 0 ; i < a.length ; i++){
b[i] = (K)a[i];
}
this.a = b;
}
}

public String toString(){
StringBuffer Str = new StringBuffer().append("[");
boolean isFirst = true;
for(int i = 0 ; i < at ; i++ ){
if(isFirst){
isFirst = false;
}else{
Str.append(" , ");
}
Str.append((K)a[i]);
}
Str.append("]");
return Str.toString();
}

@Override
setSize(a,at);
a[at] = e ;
at++;
return false;
}

@Override
public void add(int index, K element) {
if( at > index ){
a[index] = element;
}else{
throw new IndexOutOfBoundsException("Index:"+index+", Size: "+at);
}
}

@Override
public boolean addAll(Collection<? extends K> c) {
Object[] o = c.toArray();// TODO Auto-generated method stub
for(int i = 0 ; i < o.length ; i++ ){
}
return false;
}

@Override
public boolean addAll(int index, Collection<? extends K> c) {
if( (at-1) < index ){
throw new IndexOutOfBoundsException("Index:"+index+", Size: "+at);
}
Object[] o = c.toArray();// TODO Auto-generated method stub
this.at = index;
for(int i = 0 ; i < o.length ; i++ ){
}
return false;
}

@Override
public void clear() {
this.a = new Object[8];
this.at = 0;
}

@Override
public boolean contains(Object o) {
if(o != null){
for(int i = 0 ; i < at ; i++ ){
if( ((K)o).equals((K)a[i]) || (K)o == (K)a[i] ) return true ;
}
}else{
for(int i = 0 ; i < at ; i++ ){
if(a[i] == null) return true ;
}
}
return false;
}

@Override
public boolean containsAll(Collection<?> c) {
Object[] o = c.toArray();// TODO Auto-generated method stub
for(int i = 0 ; i < o.length ; i++ ){
if(!contains(o[i])) return false;
}
return true;
}

@Override
public K get(int index) {
if( at > index ){
return (K)a[index];
}else{
throw new IndexOutOfBoundsException("Index:"+index+", Size: "+at);
}
}

@Override
public int indexOf(Object o) {
int j = -1;
if(o != null){
for(int i = 0 ; i < at ; i++ ){
if(((K)o).equals((K)a[i])) return i ;
}
}else{
for(int i = 0 ; i < at ; i++ ){
if(a[i] == null) return i ;
}
}
return -1;
}

@Override
public int lastIndexOf(Object o) {
int j = -1;
if(o != null){
for(int i = (at-1) ; i > -1 ; i-- ){
if(((K)o).equals((K)a[i])) return i ;
}
}else{
for(int i = (at-1) ; i > -1 ; i-- ){
if(a[i] == null) return i ;
}
}
return -1;
}

@Override
public boolean isEmpty() {
return (at==0);
}

@Override
public Iterator<K> iterator() {
Iterator<K> i = new Iterator<K>() {
private int now = 0;
@Override
public boolean hasNext() {
return ( now < at );
}

@Override
public K next() {
K k = (K)a[now];
now++;
return k;
}

@Override
public void remove() {

}
};

return i;
}

@Override
public ListIterator<K> listIterator() {
ListIterator<K> i = new ListIterator<K>() {

private int now = 0;
@Override

}

@Override
public boolean hasNext() {
return ( now < at );
}

@Override
public boolean hasPrevious() {
return (now > 0);
}

@Override
public K next() {
K k = (K)a[now];
now++;
return k;
}

@Override
public int nextIndex() {
if(now < ( at - 1 ) ){
return now+1;
}else{
return -1;
}
}

@Override
public K previous() {
if(now > 0 ){
return (K)a[now-1];
}else{
throw new IndexOutOfBoundsException("iterator is at the begainest");
}
}

@Override
public int previousIndex() {
if(now > 0 ){
return now-1;
}else{
return -1;
}
}

@Override
public void remove() {
// TODO Auto-generated method stub

}

@Override
public void set(K e) {
// TODO Auto-generated method stub

}
};
return null;
}

@Override
public ListIterator<K> listIterator(int index) {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean remove(Object o) {
int i = indexOf(o);
while(i != -1){
remove(i);
i = indexOf(o);
};
return false;
}

@Override
public K remove(int index) {
for(int j = index ; j < at ; j++ ){
a[j] = a[j+1];
}
at--;
return null;
}

@Override
public boolean removeAll(Collection<?> c) {
Iterator<K> i = (Iterator<K>) c.iterator();
while(i.hasNext()){
remove((K)i.next());
}
return false;
}

@Override
public boolean retainAll(Collection<?> c) {
return false;
}

@Override
public K set(int index, K element) {
return null;
}

@Override
public int size() {
return at;
}

@Override
public List<K> subList(int fromIndex, int toIndex) {
List<K> result = new newArray<K>();
if( toIndex < fromIndex ){
throw new IndexOutOfBoundsException("fromIndex:"+fromIndex+", toIndex: "+toIndex);
}
if( (at-1) < fromIndex ){
throw new IndexOutOfBoundsException("fromIndex:"+fromIndex+", Size: "+at);
}
if(toIndex > at) toIndex = at;
for(int i = fromIndex ; i < toIndex ; i++){
}
return result;
}

@Override
public Object[] toArray() {
Object[] arr = new Object[at];
for(int i = 0 ; i < at ; i++){
arr[i] = a[i];
}
return arr;
}

@Override
public <T> T[] toArray(T[] a) {
a = (T[]) toArray();
return null;
}

}```

您需要登录以后才能回答，未注册用户请先注册