Reputation: 37
I have checked all examples related to this but have not been able to solve my issue.
I have created check box and if I used to unchecked means getting some values
It return
string selectedid = {"2","6","45","34","5"};
string[] separteid = Regex.Split(selectedid, ",");
List<string>idlist=new List<string>();
foreach(var item in separteid)
{
idlist.Add(item);
}
I want to check with integer ID of database table
job = (from n in bc.db.xx where idlist.Contains(n.id.ToString()) select n);
ViewBag.Count = job.Count();
How to fix it? Anyone help?
Upvotes: 1
Views: 108
Reputation: 10694
You can use following if EF version being used is 4 or above. Check this for more.
SqlFunctions.StringConvert((Decimal)n.id).Trim(),
Or else you can convert your list of string into list of int like below and use .Contains over it.
job = (from n in bc.db.xx where idlist.Select(x=>int.Parse(x)).Contains(n.id) select n);
Upvotes: 3
Reputation: 3603
A very fix in your case since you're comparing against integers on the DB side would be to pass integers on the C# side instead of passing strings and trying to convert on the other side
The issue is that most of your code doesn't make sense, you're creating a string with list syntax (i'm surprised this even compiles). Then make it into an array by spliting it, then generate a list from that array, all of this could be done in a single step, or better yet you could even declare as a list of int to begin with replacing all of this :
string selectedid = {"2","6","45","34","5"};
string[] separteid = Regex.Split(selectedid, ",");
List<string>idlist=new List<string>();
foreach(var item in separteid)
{
idlist.Add(item);
}
with this:
List<int> idlist = new List<int>(){2,6,45,34,5};
And then you already have the right type which allows you to simplify this :
job = (from n in bc.db.xx where idlist.Contains(n.id.ToString()) select n);
ViewBag.Count = job.Count();
To this :
ViewBag.Count = (from n in bc.db.xx where idlist.Contains(n.id) select n)
.Count();
Or if you don't mind lambda syntax you can directly pass the predicate to count:
ViewBag.Count = bc.db.xx
.Count(n=>idlist.Countains(n.id));
Upvotes: 2
Reputation: 3589
If typeof (n.id) == System.String
, just remove the ToString()
invocation. If the type is int, or something implicitly convertible to an int, transform your list instead:
string[] strIds = new[] {"1", "2", "3"}; // I assume you get these values from somewhere else
var idlist = strIds.Select(int.Parse);
var job = (from n in bc.db.xx
where idlist.Contains(n.id.ToString())
select n);
Upvotes: 2