2013-04-12 15 views
10

TRUE veya FALSE döndürmesi gereken bir sorgu var.Linq sorgu döndürme doğru veya yanlış

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 

ve ben

this.result = Conert.ToBoolean(query); 

nasıl LINQ bunu başarmak için (dize veri türü) bir özelliğe bu sorgu sonucunu eklemek istediğiniz?

DÜZENLEME:

EmpMapper sınıf

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;} 
    } 
    } 

MainViewModel sınıf

 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 'satır başına bir değer döndürecektir *. ToString’in aranmasının sonucunu ne istiyorsunuz? –

+0

'(query.Count()> 0) .ToString()' gibi bir şey mi arıyorsunuz? –

+0

@jon: Sanırım bir dizgeyi –

cevap

21

bu deneyin

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

Yukarıdaki sonuç, yalnızca bunu boole'ye dönüştürdüğümde çalıştı. Teşekkürler @Saptal Singh – user1221765

+0

Bu cevap benim nihayet kopyalamak için düzenlenmiştir. –

+1

Üzgünüz ama sizin kopyalanmamış, basit mantık – 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

'yi kontrol edin Burada anonim bir yazı kullanmanın bir nedeni yok (bunun bile derleneceğini sanırım) ve ToString' üzerinde kesinlikle gerekli olanı yapmayacaksınız. –

3

.Any() veya .Count() öğesini kullanabilirsiniz. Any(), daha iyi performans gösteriyor.

yapacağı her türlü()

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() (neden görmek için bu SO question edin) Sana true biri varsa almak istiyorum doğru anlamak

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

, bu özel durumda gereksiz işlem yapar gibi 'Count' kullanmamak daha iyidir –

4

Sorgunun sonuçları tüm koşullarla eşleşir. Eğer gerçekten bir "yanlış" "true" veya dize Yanıtı olmasını istiyorsanız

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

: Bu durumda böyle bir şey denemek

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(); 
     } 
    } 

Ama mülkiyet "sonucunu" yapmak için öneririm bir bool ve ToString() öğesini kaldırın. Bir boole sahip olduğunuzda her zaman bir ToString() üzerinde yapabilirsiniz