I have a table in the database as
Code No Email Pwd
-----------------------------------------------------------
ABCDEFG 1 aaa@gmail.com 1
EYETW 2 bbb@gmail.com 2
WDHH0 3 NULL NULL
DZDX220 4 NULL NULL
AL7F0MI 5 NULL NULL
Q5D6M4R 6 NULL NULL
Now, in this table I have to check if the email
used by the user already exists then I want to retrieve the row, if the email doesn't exist then I have to check if the Pwd
has been already used and retrieve the row data.
I have tried this
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();
But it is not returning the right answers.
Test cases :
If I pass aaa@gmail.com and 2 then I have to retrieve `aaa@gmail.com 1 .
If I pass bbb@gmail.com and 1 then I want to retrieve bbb@gmail.com 2 or null
.
Sorry guys, if any of them exist, I want to retrieve those values but not nulls
You need to update your Where(). It should be like:
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();
As Yacoub pointed in the comment, issue is with table.Field<string>("Email") == email
it should be == 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");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.