10

错误

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

这是我得到的错误。

控制器-

 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);
 }

局部视图

<%@ 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>

看法

<%@ 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>

复杂类

 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; }
 }

sp_help 装置的输出

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

这是我的整体结构,我得到了上述错误。我用谷歌搜索,但我没有得到一个确切的解决方案。非常感谢任何帮助。

感谢所有人的帮助,我特别感谢Jon Skeet提出的想法

请查看我更新的查询

 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))

                   };

我使用SqlFunctions.StringConvert 转换为字符串,现在它可以工作了。谢谢大家。

4

4 回答 4

3

代替:

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

尝试:

<% foreach (var item in Model)

还尝试使用以下.ToList()方法急切地加载您的实体:

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

您也可以考虑使用视图模型而不是ViewData. 它将使您的代码更清晰,您将不再依赖魔术字符串


更新:

试试这个:

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 + ""
    });
于 2012-07-05T05:50:42.160 回答
2

尝试重写您的 LINQ 查询:

var fixtures = Oritia_entities.Fixtures
    .Where(fixtures => fixtures.SeasonId == seasionID)
    .AsEnumerable()
    .Select(fixtures => 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 + ""
                    }
    )
    .ToList();

LINQ 查询基本相同,但我在可以转换为 SQL 的最后一个条件之间放置了一个 AsEnumerable() 调用(即 Where(...),顺便说一句,seasionID 的类型和值是什么? ) 和 Select(...)。

原因是我认为 L2S 以某种方式试图将 Select(..) 转换为 SQL。不确定它会起作用,但值得一试。

于 2012-07-05T06:33:05.000 回答
0

在控制器中使用它:

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

我不确定,但我认为这就是你的问题所在。如果您不使用ToArray,那么您将向视图发送IQueryable,这可能会出现问题。

于 2012-07-05T05:59:10.630 回答
0

试一试……(虽然 id 稍微调整了 try catch,如果你最终使用它。我不建议抑制错误并且不做任何事情/让它无声无息地死去;P)

 public string NullableObjectToString(object obj)
 {
      try
      {
          if (obj == null)
              return "";

          return (obj ?? string.Empty).ToString();
      }
      catch (Exception)
      {
          return "";
      }
  }

然后

var fixtures = from fixtures in Oritia_entities.Fixtures
                  where fixtures.SeasonId == seasionID
                  select new FixtureModel
                  {                        
                     Winners = NullableObjectToString(fixtures.TeamWon),
                     FirstBattingTeam = NullableObjectToString(fixtures.FirstBattingTeam) // ..... you get the idea
                  }
于 2012-07-05T06:50:05.303 回答