The type ' system.nullable ' 1 cannot be cast to type ' System.Object '. LINQ to entities only supports casting EDM primitives or enumeration types.

Source: Internet
Author: User

When using LINQ and EF in a project, there was a problem with the exception shown in the topic, and the search for a lot of data found no solution, mainly because of the lack of knowledge of EF.

The situation is recorded as follows for future reference.

The query primarily designs two tables, which are associated by foreign keys:

  

An exception occurred while making the following query: Cannot cast type ' system.nullable ' 1 ' to type ' System.Object '. LINQ to entities only supports casting EDM primitives or enumeration types.

  PublicActionResult getipsegments () {//list<ipsegment> ipsegments = (from S in devicedb.ipsegment select s). ToList ();            varIpsegments = fromDinchDevicedb.devicecategory Join Sinchdevicedb.ipsegment on D.devicecategoryid equals S.devicecategoryid Select New{devicecategoryid1=S.devicecategoryid, Devicecategoryname1=D.devicecategoryname, Ipsegment="202.115.242."+ S.ip_head +"-202.115.242."+S.ip_end}; returnJson (ipsegments, jsonrequestbehavior.allowget); }

Later, the query was modified to be successful. The modified query looks like this:

     Public actionresult getipsegments ()        {            var from in devicedb.ipsegment                             Selectnew                              {                                 S.devicecategoryid,                                 s.devicecategory.devicecategoryname,                                 s.ip_head,                                 s.ip_end                             };             return Json (ipsegments, jsonrequestbehavior.allowget);        }

The reasons for this are not yet understood, and then supplemented by understanding.

The

Cannot cast the type "System.Nullable ' 1" to type "System.Object". LINQ to entities only supports casting EDM primitives or enumeration types.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.