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.

Layla
  • 47
  • 7
  • What do you mean by decimal values? what's the error are you facing? – Mox Shah Nov 23 '15 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)) } – Layla Nov 23 '15 at 16:01
  • Possible duplicate of [search decimal values in Linq query](http://stackoverflow.com/questions/29622158/search-decimal-values-in-linq-query) – Mox Shah Nov 23 '15 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. – Mox Shah Nov 23 '15 at 16:07
  • What EF version are you targeting? – Ivan Stoev Nov 23 '15 at 16:18

3 Answers3

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
            };
Yazan Ati
  • 126
  • 1
  • 3
  • How to do multiple values? and what should I change in: if(!String.IsNullOrEmpty(searchString))? – Layla Nov 23 '15 at 16:14
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();
khaled saleh
  • 470
  • 7
  • 18
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!

NekoMisaki
  • 101
  • 1
  • 6