Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8195711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:13:45+00:00 2026-06-07T05:13:45+00:00

ERROR Unable to cast the type ‘System.Nullable`1’ to type ‘System.Object’. LINQ to Entities only

  • 0

ERROR

Unable to cast the type 'System.Nullable`1' to type 'System.Object'.
 LINQ to Entities only supports casting Entity Data Model primitive types.

This is the error I am getting.

Controller –

 public ActionResult FixturesAll()
 {
     teamMgr = new TeamManager();
     fixtureMgr = new FixtureManager();

     var team = teamMgr.GetTeams();
     var viewModel = new TeamIndexViewModel()
     {
          Teams = team.ToList(),
          NumberOfTeams = team.Count()
     };

     var fixtures = from fixtures in Oritia_entities.Fixtures
                    where fixtures.SeasonId == seasionID
                    select new FixtureModel
                    {

                        Team1 = "",
                        Team2 = "",
                        Winners = (fixtures.TeamWon+""),
                        FirstBattingTeam = (fixtures.FirstBattingTeam+""),
                        SecondBattingTeam = (fixtures.SecondBattingTeam+""),
                        Team1Score = fixtures.Team1Score + "",
                        Team1Wickets = fixtures.Team1Wickets + "",
                        Team2Score = fixtures.Team2Score + "",
                        Team2Wickets = fixtures.Team2Wickets + ""
                    };

      ViewData["Fixtures"] = fixtures;
      return View(viewModel);
 }

Partial View

<%@ Control Language="C#" Inherits=
"System.Web.Mvc.ViewUserControl<IEnumerable<DataAccess.FixtureModel>>" %>
    <table>
    <% foreach (var item in ViewData["Fixtures"]
 as IEnumerable<DataAccess.FixtureModel>) // Here I am getting the error
       { %>
          <tr>                 
            <td>
                <%: item.Team1 %>
            </td>
            <td>
                <%: item.Team2 %>
            </td>
          </tr>
   </table>

View

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
 Inherits="System.Web.Mvc.ViewPage<FunBox.ViewModels.TeamIndexViewModel>" %>
<ul>
    <% foreach (string team in Model.Teams)
       { %>
           <li><a href="<%: team.ToString() %>/">
           <%: team.ToString()  %></a> </li>
    <% } %>
</ul>

<div>
        <% Html.RenderPartial("FixturesAll",ViewData["Fixtures"]); %>
    </div>

Complex Classes

 public class TeamIndexViewModel
 {
     public int NumberOfTeams { get; set; }
     public List<String> Teams { get; set; }
 }

 public class FixtureModel
 {
     public string Team1 { get; set; }
     public string Team2 { get; set; }
     public string Winners { get; set; }
     public string Team1Score { get; set; }
     public string Team1Wickets { get; set; }
     public string Team2Score { get; set; }
     public string Team2Wickets { get; set; }
     public string FirstBattingTeam { get; set; }
     public string SecondBattingTeam { get; set; }
 }

Output of sp_help Fixtures

Id          bigint (pk)
TeamID1     bigint
TeamID2         bigint
TeamWon         bigint
Date            datetime
SeasonId    bigint
ManOfTheMatch   bigint
FirstBattingTeam    bigint
SecondBattingTeam   bigint
ResultDescription   nvarchar
Team1Score  bigint
Team2Score  bigint
Team1Wickets    bigint
Team2Wickets    bigint

This is my overall structure and I am getting the above error.I googled but i didn’t get an exact solution for this. Any help is highly appreciated.

Thanks all for helping me and my special thanks to Jon Skeet for giving the idea

Please see my updated query

 var data = from fixtures in Oritia_entities.Fixtures
                   join t1 in Oritia_entities.Teams on new { ID = fixtures.TeamID1 } equals new { ID = t1.ID }
                   join t2 in Oritia_entities.Teams on new { ID = fixtures.TeamID2 } equals new { ID = t2.ID }
                   where fixtures.SeasonId == seasionID
                   select new FixtureModel
                   {
                       Team1 = t1.TeamName,
                       Team2 = t2.TeamName,

                       Winners = SqlFunctions.StringConvert((double)(fixtures.TeamWon ?? 1)),
                       FirstBattingTeam = SqlFunctions.StringConvert((double)(fixtures.FirstBattingTeam ?? 1)),
                       SecondBattingTeam = SqlFunctions.StringConvert((double)(fixtures.SecondBattingTeam ?? 1)),
                       Team1Score = SqlFunctions.StringConvert((double)(fixtures.Team1Score ?? 1)),
                       Team1Wickets = SqlFunctions.StringConvert((double)(fixtures.Team1Wickets ?? 1)),
                       Team2Score = SqlFunctions.StringConvert((double)(fixtures.Team2Score ?? 1)),
                       Team2Wickets = SqlFunctions.StringConvert((double)(fixtures.Team2Wickets ?? 1))

                   };

I used SqlFunctions.StringConvert for converstion to string and nows it working. Thanks all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:13:47+00:00Added an answer on June 7, 2026 at 5:13 am

    Instead of:

    <% foreach (var item in ViewData["Fixtures"] as IEnumerable<DataAccess.FixtureModel>)
    

    try:

    <% foreach (var item in Model)
    

    Also try to eagerly load your entities using the .ToList() method:

    ViewData["Fixtures"] = fixtures.ToList();
    

    Also you might consider using view models instead of ViewData. It will make your code much cleaner and you will no longer rely on magic strings.


    UPDATE:

    Try this:

    var fixtures = Oritia_entities
        .Fixtures
        .Where(f => f.SeasonId == seasionID)
        .ToList()
        .Select(f => new FixtureModel
        {
            Team1 = "",
            Team2 = "",
            Winners = (f.TeamWon+""),
            FirstBattingTeam = (f.FirstBattingTeam+""),
            SecondBattingTeam = (f.SecondBattingTeam+""),
            Team1Score = f.Team1Score + "",
            Team1Wickets = f.Team1Wickets + "",
            Team2Score = f.Team2Score + "",
            Team2Wickets = f.Team2Wickets + ""
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to solved error Unable to cast object of type 'System.Int32' to type 'Microsoft.DirectX.Direct3D.VertexShader'
selectCourseList.SelectedIndex=Convert.ToInt32(selectCourseList.Items.FindByValue(newStudentCourseId)); giving following error Unable to cast object of type 'System.Web.UI.WebControls.ListItem' to type 'System.IConvertible'.
I'm getting this error Unable to cast object of type 'System.Int32' to type 'System.String'.
I am seeing this error: Unable to cast object of type 'System.Web.Profile.DefaultProfile' to type
Hey Just getting the following error Unable to cast object of type 'System.Object[]' to
Error : Unable to cast object of type 'System.String' to type 'System.Windows.Forms.ListBox'. private void
it will give me error Unable to cast object of type . public class
Any ideas why does the entity framework in LINQ gives following strange error: Unable
Why do I get the error: Unable to create a constant value of type
i have written the following query and it is giving error Unable to cast

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.