1

My table have 5 columns:

public partial class NPG_Chemical_DOT_and_Guide_Numbers
{
    [Key]
    [Column(TypeName = "numeric")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public decimal NPG_Chemical_DOT_and_Guide_Numbers_ID { get; set; }

    [Column(TypeName = "numeric")]
    public decimal NPG_Chemical_ID { get; set; }

    [StringLength(64)]
    public string DOT_Number { get; set; }

    public string Guide_Number { get; set; }

    [StringLength(128)]
    public string Conditional { get; set; }
}

Now I can search text values in the table by using:

public ActionResult Index(string searchString)
    {
        var search = from m in db.NPG_Chemical_DOT_and_Guide_Numbers
                     select m;
        if (!String.IsNullOrEmpty(searchString))
        {
            search = search.Where(s => s.DOT_Number.Contains(searchString) || s.Guide_Number.Contains(searchString) ||s.Conditional.Contains(searchString));
        }
        return View(search);
}

and:

<p>
@using (Html.BeginForm())
{
<p>
    Title: @Html.TextBox("SearchString")
    <input type="submit" value="Search" />
</p>
}
</p>

The question is how to search the decimal values? I tried to use convert to string, but shown LINQ error.

5
  • What do you mean by decimal values? what's the error are you facing? Commented Nov 23, 2015 at 15:57
  • The first 2 columns are decimal type not string type, so I cannot search them by using: if (!String.IsNullOrEmpty(searchString)) { search = search.Where(s=>s.DOT_Number.Contains(searchString)) } Commented Nov 23, 2015 at 16:01
  • Possible duplicate of search decimal values in Linq query Commented Nov 23, 2015 at 16:05
  • Layla, You can have a look on the answer posted by @YazanAti or another I've provided in above comment, let us know if these answers doesn't work for you. Commented Nov 23, 2015 at 16:07
  • What EF version are you targeting? Commented Nov 23, 2015 at 16:18

3 Answers 3

1

you can use SqlFunctions.StringConvert. There is no overload for int so you need to cast to a double or a decimal. Your code ends up looking like this:

var items = from c in contacts
            select new ListItem
            {
                Value = SqlFunctions.StringConvert((double)c.ContactId).Trim(),
                Text = c.Name
            };
Sign up to request clarification or add additional context in comments.

1 Comment

How to do multiple values? and what should I change in: if(!String.IsNullOrEmpty(searchString))?
0

try this solution

String SearchWord = "500.4";    
decimal searchBy; //define a decimal variable to hold the Search By value
decimal.TryParse(SearchData, out searchBy); //try parse it to decimal from string

And in your query do this:

Employees = _entities.Employees.Where(p.Salary==searchBy)).ToList();

Comments

0

Exactly as searching a string but with some changes:

public ActionResult Index(string searchString){
    searchString = searchString.Replace(',', '.');

    var search = from m in db.NPG_Chemical_DOT_and_Guide_Numbers
                 select m;

    if (!String.IsNullOrEmpty(searchString)){
        search = search.Where(s => (s.DOT_Number.ToString()).Contains(searchString)
                 || (s.Guide_Number.ToString()).Contains(searchString)
                 || s.Conditional.Contains(searchString));
    }
    return View(search);
}

With this code line: (s.DOT_Number.ToString()).Contains(searchString) you can search the column, but the notation . instead of ,

The Replace function above is resposible for comfortable search without noticing the change between , and .

so you can search with notation , without noticing that the program replacing it in background (user-friendly version)

I hope it helped!

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.