Hello
I am trying to fill a dropdown list with the values of my tables. I don't need all of the values so I have to use a "where" clause
here is my code
var tids = db.mytable.AsEnumerable()
.Select(p => Convert.ToInt32(Regex.Replace(p.myID, "[^0-9]+", string.Empty)))
.ToArray();
var query = (from e in tids
where e >= 1 && e <= 8
select new MyTableViewModel ()
{
ID = Convert.ToInt32(Regex.Replace(db.mytable.Find(e).myID, "[^0-9]+", string.Empty)),
Details= db.mytable.Find(e).myDetails
}).ToList();
SelectList list = new SelectList(query , "ID", "Details");
ViewBag.ddl = list;
I my table (myID field) I have values such as 01,02, 03-01-02,01-02 etc.
I get the error
The type of one of the primary key values did not match the type defined in the entity
and in the details:
"The argument types 'Edm.String' and 'Edm.Int32' are incompatible for this operation. Near WHERE predicate, line 1, column 66."
My VM
is
public class MyTableViewModel
{
public int ID { get; set; }
public string Details { get; set; }
}
I am searching for the error details but nothing helped me
thank you
Looking at the error message
The argument types 'Edm.Int64' and 'Edm.String' are incompatible for this operation. Near WHERE predicate, line 1, column 66..
It seems like you are working with the wrong type of data.
The error seems to be in line
Convert.ToInt32(Regex.Replace(p.myID, "[^0-9]+", string.Empty))
I don't understand what you are trying to achieve from above code but p.myID
is of type int and you are trying to implement Regex code as it is on type string.
You can't use the above query of int type of data as a string, that's why you are getting error.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly