Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare int with string in linq where clause [duplicate]

I have a string parameter which that i want to compare with int and string in Linq query.

var query=(from GRD in _tblMembers.GetQueryable()
           select new MembersModel
           {
               SchoolID = GRD.SchoolID,
               MemberID = GRD.MemberID,
               MemberReferenceID = GRD.MemberReferenceID,
               MemberTypeID = GRD.MemberTypeID,
               MemberNo = GRD.MemberNo,
               MemberName = GRD.MemberName,
               CellNo = GRD.CellNo,                                         
            }).Where(z => z.MemberName.Contains(param.sSearch) || z.MemberTypeID.Contains(param.sSearch)).ToString();

when i tried to convert database parameter to to string type using ToString() method as follows.

.Where(z => z.MemberName.Contains(param.sSearch) || z.MemberTypeID.ToString().Contains(param.sSearch)).ToString();

linq throw run time exception.

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

How can i do coversion in linq query.

like image 467
Muhammad Nasir Avatar asked Dec 18 '15 08:12

Muhammad Nasir


1 Answers

You can use SqlFunctions.StringConvert

.Where(z => z.MemberName.Contains(param.sSearch) 
            || SqlFunctions.StringConvert((double)z.MemberTypeID).Contains(param.sSearch))
.ToString();
like image 70
Domysee Avatar answered Nov 14 '22 23:11

Domysee