5

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.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
Muhammad Nasir
  • 2,126
  • 4
  • 35
  • 63

1 Answers1

7

You can use SqlFunctions.StringConvert

.Where(z => z.MemberName.Contains(param.sSearch) 
            || SqlFunctions.StringConvert((double)z.MemberTypeID).Contains(param.sSearch))
.ToString();
Domysee
  • 12,718
  • 10
  • 53
  • 84
  • `var query = (from c in ctx.A where c.ID==cl select new Accoun { ex = c.Type }).Distinct();` Here `cl` is `int` and c.ID is `string` but I want to compare them in `int` format. I have tried with `where Convert.ToInt32(c.ID)==cl` but getting `invalid where condition` error – Krish Nov 08 '17 at 07:40