1

i'm a Asp.net MVC Develpoer and i have problem with converting string to int because i
because i nedd it on search. When i tryed to write this code on the Contoller:

public ViewResult Search(string textboxmvc)
{
    var student = from i in db.StudentSet  select i;

    if (!String.IsNullOrEmpty(textboxmvc))
    {
        student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                                   || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==int.Parse(textboxmvc));

    }

     return View(student);

}

it shows this problem:

Server Error in '/' Application.
LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NotSupportedException: LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression.

Source Error:


Line 42:                     </tr>
Line 43: 
Line 44:                 <% foreach (var item in Model) { %>
Line 45:                     <tr>
Line 46:                    


Source File: d:\aimen\Projects\Schoo_Project\Schoo_Project\Views\Students\Search.aspx    Line: 44 
0

8 Answers 8

3

You have to parse it beforehand.

public ViewResult Search(string textboxmvc)
{
    int parsedId;
    int.TryParse(textboxmvc, out parsedId);
    var student = from i in db.StudentSet  select i;

    if (!String.IsNullOrEmpty(textboxmvc))
    {
        student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                                   || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==parsedId);

    }

     return View(student);

}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for int.TryParse, since it appears same textboxmvc is used for getting name as well as ID
1

Assign it to another variable first.

if (!String.IsNullOrEmpty(textboxmvc))
{
    var textboxmvcAsInt = int.Parse(textboxmvc);
    student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                               || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==textboxmvcAsInt);

}

Comments

0

Try this:

public ViewResult Search(string textboxmvc)
{
    var student = from i in db.StudentSet  select i;

    if (!String.IsNullOrEmpty(textboxmvc))
    {
        int val = int.Parse(textboxmvc);
        student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                                   || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==val);

    }

     return View(student);

}

Comments

0

Put the conversion outside of the LINQ expression:

var numericValue = int.Parse(textboxmvc);

student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                           || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==numericValue);

    }

Comments

0

Pares textbox content to int outside LINQ to Entities query:

if (!String.IsNullOrEmpty(textboxmvc))
{
    var id = int.Parse(textboxmvc)
    student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                               || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())
                               || s.Id == id);

}

Comments

0

LINQ doesn't know how to convert Int.Parse to SQL code. Do the cast and assign it to a variable before the query.

Comments

0
 if (!String.IsNullOrEmpty(textboxmvc))
 {
    int i = int.Parse(textboxmvc);
    student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())     
                 ||    s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==i);

  }

Comments

0

I think that you have to make the conversion before Where like this:

if (!String.IsNullOrEmpty(textboxmvc))
    {
        var number = int.Parse(textboxmvc);
        student = student.Where(s => s.FirstName.ToUpper().Contains(textboxmvc.ToUpper())
                                   || s.LastName.ToUpper().Contains(textboxmvc.ToUpper())||s.Id==number);

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.