The type of one of the primary key values did not match the type defined in the entity


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


Asked by:- mspace
0
: 4673 At:- 5/7/2018 8:09:39 AM
mvc C#







1 Answers
profileImage Answered by:- neena

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.

1
At:- 5/7/2018 8:55:42 AM Updated at:- 5/7/2018 12:46:49 PM
I want to grab the ids that are greater than 01 and smaller than 08 but some of my records are 01-02-03, 01-02 etc 0
By : mspace - at :- 5/7/2018 9:00:00 AM
Do I have to convert them fist to int and then to remove the dash? Is that a right logic or not? thank you 0
By : mspace - at :- 5/7/2018 9:22:53 AM
Yes, you should first split your string by '-', convert string numbers (01,02,09 etc) into numbers then work with int's, instead of strings 0
By : neena - at :- 5/7/2018 12:48:17 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use