简体   繁体   中英

LINQ OR operator not giving result

I have written a LINQ query with or condition in it, but its not working, it seem I am doing something wrong.

I am passing a string value and on it, I want to get my result.

var userDetails = context.tbl_members.Where
                (
                  d => d.Mobile == value 
                  || 
                  d.MemberId == Int32.Parse(value)
                ).SingleOrDefault();

its not working if someone put a mobile no, but if work with memberID

if I split the query keep only mobile no its running fine.

var userDetails = context.tbl_members.Where(d => d.Mobile == value ).SingleOrDefault();

Please check what I did wrong with or condition

Regards Moksha

var userDetails = context.tbl_members
                         .Where(d => d.Mobile == value || 
                                     d.MemberId == Int32.Parse(value))
                         .SingleOrDefault();

It looks like you are using Linq to Entities or Linq to Sql. Int32.Parse() is not supported in that context - just do the number conversion before your query:

int numValue = Int32.Parse(value);
var userDetails = context.tbl_members
                         .Where(d => d.Mobile == value || d.MemberId == numValue)
                         .SingleOrDefault();

thanks for your help brokenGlass,

the error was in converting from string to int, as I was passing string of 10 digits, it was not getting converted into int

Value was either too large or too small for an Int32.

thanks

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM