2016-04-16 54 views
0

條件我都在數據庫中的表作爲凡使用LINQ

Code   No     Email   Pwd 

ABCDEFG   1    [email protected]   1 
EYETW   2    [email protected]   2 
WDHH0   3     NULL    NULL 
DZDX220   4     NULL    NULL 
AL7F0MI   5     NULL    NULL 
Q5D6M4R   6     NULL    NULL 

現在,在這個表我要檢查,如果用戶使用的email已經存在,那麼我想retieve行,如果電子郵件不存在,那麼我必須檢查Pwd是否已被使用,並檢索行數據。

我已經試過這

var data = (from table in ds.Tables[0].AsEnumerable() 
          where table.Field<Int16>("Pwd") == Convert.ToInt16(pwd) || table.Field<string>("Email") != email 
          select new {emailAddress = table.Field<string>("email") , passsword = table.Field<Int16>("Pwd") }).FirstOrDefault(); 

但它沒有做出正確的回答。

測試用例:

如果我通過[email protected]和2那麼我必須找回`[email protected] 1。

如果我通過[email protected]和1,那麼我想檢索[email protected] 2 or null

對不起球員,如果其中任何exiss我婉檢索這些值而不是空值

回答

0

你需要更新你在哪裏()。它應該是這樣的:

var data = (from table in ds.Tables[0].AsEnumerable() 
      where (table.Field<string>("Email") == email) //email exists 
      || (table.Field<string>("Email") != email && table.Field<Int16>("Pwd") == Convert.ToInt16(pwd)) //email doesn't exist, but password does 
      select new {emailAddress = table.Field<string>("email") , passsword = table.Field<Int16>("Pwd") }).FirstOrDefault(); 
+0

我很抱歉,這是我的錯誤。如果它們中的任何一個存在,我想檢索這些值但不爲空。 @swamibabulal –

0

由於雅各布在註釋中指出,問題是table.Field<string>("Email") == email應該== email

var matchingrow = ds.Tables[0].AsEnumerable() 
           .FirstOrDefault(table=> table.Field<Int16>("Pwd") == Convert.ToInt16(pwd) 
                || table.Field<string>("Email") == email); 

if(matchingrow != null) 
{ 
    // matching email/pwd. logic goes here 

    // matchingrow.Field<string>("Email"); 
} 
+0

對不起,這是我的錯誤。如果它們中的任何一個存在,我想檢索這些值,但不是null。@ Hari Prasad –