2013-04-12 9 views
10

J'ai une requête où retourner TRUE ou FALSE.Requête Linq return true or false

var query = from c in db.Emp 
      from d in db.EmpDetails 
      where c.ID == d.ID && c.FirstName == "A" && c.LastName == "D" 
      // It should return TRUE when this above statement matches all these conditions 

et je veux joindre ce résultat de la requête à une propriété (de type de données string)

this.result = Conert.ToBoolean(query); 

comment y parvenir dans LINQ?

EDIT:

classe EmpMapper

public class EmpMapper 
    { 
    EmpEntities db; 
    // ID column already exists in the DB 
    private int ID; 
    // I am creating this property to add it from the UI side, depending on the certain conditions in the query. That is why I created a separate class to map the existing ID from the DB 
    bool result; 
    public EmpMapper(int ID, bool result) 
    { 
     this.db = new EmpEntites(); 
     this.ID = ID; 
     var query = from c in db.Emp 
      from d in db.EmpDetails 
      where c.ID == d.ID && c.FirstName == "A" && c.LastName == "D" 
      // It should return TRUE when this above statement matches all these conditions 
     this.result = Convert.ToBoolean(query); 
    } 
    public int ID 
    { 
    get{return this.ID;} 
    set{this.ID = value;} 
    } 
    public bool result 
    { 
    get{return this.result;} 
    set{this.result = value;} 
    } 
    } 

classe MainViewModel

 List<EmpMapper> empMapCol = new List<EmpMapper>(); 

    private void Page_Loaded(object sender, RoutedEventArgs e) 
    { 
     var emp_query = from c in db.Emp 
         orderby c.ID 
         select a; 
    List<Emp> empCol = emp_query.ToList(); 
    foreach(Emp item in empCol) 
    { 
     this.empMapCol.Add(new EmpMapper(item.ID, item.result)); 
    } 
    datagrid1.ItemsSource = empMapCol; 
    } 
    } 
+1

'query' retournera une valeur * par ligne *. Que voudriez-vous que le résultat de l'appel 'ToString' soit? –

+0

Cherchez-vous quelque chose comme '(query.Count()> 0) .ToString()'? –

+0

@jon: je suppose qu'il attend un vrai/faux comme une chaîne –

Répondre

21

essayer,

var query = (from c in db.Emp 
     from d in db.EmpDetails 
     where c.ID == d.ID && c.FirstName == "A" && c.LastName == "D" 
     select c 
     ).Any(); 

    this.result = query; //no need to convert to boolean its already bool value 
+0

Le résultat ci-dessus a fonctionné pour moi seulement quand je le convertis en booléen. Merci @Saptal Singh – user1221765

+0

Cette réponse a été éditée pour finalement copier le mien. –

+1

désolé mais pas copié le vôtre, sa logique simple – Satpal

0
var query = from c in db.Emp 
      from d in db.EmpDetails 
      select new { c.ID == y.ID && 
         c.FirstName == "A" && 
         c.LastName == "D" }; 
+1

Il n'y a aucune raison d'utiliser un type anonyme ici (je doute que cela compilera même) et appelant ' ToString »ne va certainement pas faire ce qui est nécessaire. –

3

Vous pouvez utiliser .Any() ou .Count(). Any() fonctionne mieux. (Cochez cette SO question pour voir pourquoi)

.Tout()

var query = from c in db.Emp 
      from d in db.EmpDetails 
      where c.ID == d.ID && c.FirstName == "A" && c.LastName == "D" 
      // It should return TRUE when this above statement matches all these conditions 
this.result = query.Any().ToString() 

.Count()

var query = from c in db.Emp 
      from d in db.EmpDetails 
      where c.ID == d.ID && c.FirstName == "A" && c.LastName == "D" 
      // It should return TRUE when this above statement matches all these conditions 
this.result = (query.Count() > 0).ToString() 
+3

il est préférable de ne pas utiliser 'Count' car il fait un traitement inutile dans ce cas particulier –

4

Si je vous comprends bien, vous voulez obtenir true si l'on des résultats de la requête correspond à toutes les conditions. Dans ce cas, essayez quelque chose comme ceci:

var found = 
    (from c in db.Emp 
    from d in db.EmpDetails 
    where c.ID == y.ID && c.FirstName == "A" && c.LastName == "D" 
    select c).Any(); 

this.result = found.ToString(); 
0

Si vous voulez vraiment avoir un « vrai » ou « false » Réponse de chaîne:

public string result 
    { 
     get 
     { 
      return db.Emp.SelectMany(c => db.EmpDetails, (c, d) => new {c, d}) 
         .Where(@t => c.ID == y.ID && c.FirstName == "A" && c.LastName == "D") 
         .Select(@t => c)).Any().ToString(); 
     } 
    } 

Mais je suggère de rendre la propriété « résultat » un booléen et supprime le ToString(). Une fois que vous avez un bool, vous pouvez toujours faire un ToString() dessus