2010-10-26 4 views
-2

Comment faire?Demande LINQ

int[] mas={1,2,3,4,...,n} 

public var method1(mas) 
{ 
var d = from i in Object where i.number==mas[0] || i.number==mas[1] || i.number==mas[2]|| ... || i.number==mas[n] select i; 
return d; 
} 
+1

Comment faire quoi? –

+1

Ce n'est pas la question la mieux écrite, mais c'est plutôt évident ce qu'il essaie de faire. – msarchet

Répondre

2

Vous voulez faire quelque chose comme ça

var d = From i in Object 
     From n in mas 
     Where n == i.Number 
     Select i; 
return d; 

En fait, maintenant que je pense à ce sujet, qui va revenir à vous une liste de i pour chaque match.

Vous avez probablement recherchez quelque chose comme

//create a list for the items that match the criteria 
    List<ObjectToGet> d = new List<ObjectToGet>; 

    //Loop over each item in your Object 
    foreach(ObjectToGet objectItem in Object){ 
     //If the item contains any match add it to the list 
     if((From n in mas Where n == d.Number Select n).Any){ 
      d.Add(objectItem); 
     } 
    } 

    return d; 

Il y a probablement une façon d'écrire ceci dans LINQ pur mais est conceptuellement ce que vous essayez de faire

0

Enveloppez votre mas dans HashSet en utilisant constructor et utiliser Contains

Vérifiez cet article comme Inroduction: Introducing HashSet (Kim Hamilton)

Alors vous allez finir avec quelque chose comme ça:

int[] mas={1,2,3,4,...,n}; 
var set = new HashSet<int>(mas); // or you can init set with proper value without array 

public var method1(mas) 
{ 
    var d = from i in Object where set.Contains(i.number) select i; 
    return d; 
} 

La solution Pure LINQ est bonne aussi (@msarchet) quand vous avez une source assez petite rayer.