简体   繁体   中英

How to convert an 'int' data type item to 'string' and search in a string list using 'Contains' in LINQ?

I am working on a project which uses ASP.net web forms and C# entity framework using LINQ. I am retrieving an 'int' type data list from one function and Check it in another string list using 'Contains'. There I have used

'SqlFunctions.StringConvert((double)MY_INT_ITEM).Trim()'

to convert that int type item (ID) to the string. It worked before but now I forget how I did it. Now when I am trying to do that it gives the following error,


"Unable to cast the type 'System. Nullable`1' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types."

Why am I getting this error and how can I fix that? Here I have attached my code. Thank you.

public static List<SESAssignmentsType> getNotUplodedUnitDetailsList(List<string> possibleUnits)
    {
        using (SESEntities db = new SESEntities())
        {
            var properUnitList = (from s in db.SES_SCHEDULE_LECTURE
                                  join u in db.SES_UNDERGRADUATE_UNIT_DETAILS on s.SCH_UNIT_ID equals u.ID
                                  where possibleUnits.Contains(SqlFunctions.StringConvert((double)s.ID).Trim())
                                  select new SESAssignmentsType
                                  {
                                      ID = s.ID,
                                      UNIT_NAME = u.UND_UNIT_NAME + " - " + a.SCH_START_DATE + " - " + a.SCH_START_TIME+ " ( Started " +
                                              SqlFunctions.DateName("day", a.SCH_START_DATE) + "/" + SqlFunctions.StringConvert((double)a.SCH_START_DATE.Value.Month).TrimStart() + "/" + SqlFunctions.DateName("year", a.SCH_START_DATE)


                                  }).ToList();


            return properUnitList;
        }
    }

Thank you very much for your help. Finally, I found the issue it is because some of the values concatenated in UNIT_NAME is nullable. @Rahul Sharma thank you very much for your idea to check nullable.

Check this after convert S.ID in to string in contains use like this below example possibleUnits.Contains(s.ID.ToString())

It looks for me that s.ID is a nullable, which means you should change it to this:

where possibleUnits.Contains(SqlFunctions.StringConvert((double)s.ID.Value).Trim())

Note that this doesnt check if s.ID is null, so you need to do that by yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM