C# “这是没有道理的。”;指定的强制转换无效;在这种情况下。还是这样?
我无法理解为什么服务器出现错误,并将注意力吸引到下面代码段中的C# “这是没有道理的。”;指定的强制转换无效;在这种情况下。还是这样?,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我无法理解为什么服务器出现错误,并将注意力吸引到下面代码段中的foreach循环 using Microsoft.CSharp; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.IO; using System.Data.Linq; using System.Data.Linq.Mapping; using
foreach
循环
using Microsoft.CSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using AllyPortal.Models;
using System.Web.Script.Serialization;
namespace AllyPortal.Controllers
{
public class AssetBundleController : Controller
{
[HttpGet]
public ActionResult Index(string bguid)
{
// bguid: bundle guid, the guid corresponding to one or more assets in the table
PortalData PD = new PortalData();
ViewBag.inputtedGuid = bguid;
ViewBag.checkedAssetLinks = new List<string> ();
foreach (AssetLink thislink in PD.links)
{
if (thislink.linkguid.Equals(bguid, StringComparison.Ordinal))
{
AssetFile thisAssetFile = PD.files.FirstOrDefault(f => f.fileid == thislink.fileid);
ViewBag.links.Add("../../Assets/" + PD.getFullFileName(thisAssetFile));
}
}
return View();
}
}
}
里面
public class PortalData : DataContext
{
...
}
还有一节课
[Table( Name = "files" )]
public class AssetFile
{
public AssetFile()
{
}
[Column(IsPrimaryKey = true, IsDbGenerated = true)]
public int fileid { get; set; }
[Column]
public int orgid { get; set; }
[Column]
public int catid { get; set; }
[Column]
public string filename { get; set; }
}
我曾经用它来迭代
foreach (AssetFile f in PD.files)
{
....
}
在另一个控制器中,这一个有什么不同之处,可能会导致错误?让我知道,如果我需要发布更多的代码,给你一个参考点
死亡的黄屏:
描述:在执行过程中发生未处理的异常
当前的web请求。请查看堆栈跟踪以了解更多信息
有关错误的信息及其在代码中的来源
异常详细信息:System.InvalidCastException:未指定强制转换
有效
源错误:
第26行:ViewBag.AssetLink=新列表();线
27:第28行:foreach(资产将此链接链接链接到
PD.links)第29行:{第30行:
源文件:c:\Users\me\Documents\Visual Studio
2013\Projects\someproj\someproj\Controllers\AssetBundleController.cs
电话号码:28
堆栈跟踪:
[InvalidCastException:指定的强制转换无效。]System.Data.SqlClient.SqlBuffer.get_Int32()+5317537
System.Data.SqlClient.SqlDataReader.GetInt32(int32i)+62
查看堆栈跟踪,此处出现错误:
System.Data.SqlClient.SqlBuffer.get_Int32() +5317537
在尝试枚举PD.links
时。这似乎表明您从源中获取的int
属性的类型不同。请检查links
表中的linkid
和fileid
的数据类型,并确保它们都是整数类型。检查代码
foreach (AssetLink thislink in PD.links)
为每次迭代执行thisLink
对象的强制转换。检查PD的类型。Links
-一个IEnumerable
可以返回任何对象
,IEnumerable
将返回任何T
类型的对象,这些对象可能比AssetLink
更派生,因此包含一些非资产链接
如果希望筛选到给定类型,而不是期望所有值都匹配,请尝试更改为
using System.Linq;
//...
foreach(var thisLink in PD.Links.OfType<AssetLink>())
使用System.Linq;
//...
foreach(在PD.Links.OfType()中变量thisLink)
因为这将只返回已经
AssetLink
s的对象,并跳过其他所有内容。错误是什么?发生在哪里?您需要提供关于实际问题的更具体信息。哪一行导致了错误?我说是foreach
循环,foreach(PD.links中的AssetLink thislink)
。我将为你们粘贴死亡的黄色屏幕。你们已经编辑了拼图的一个关键部分——PortalData.links
。该属性的类型是什么?要么你们有多个AssetLink
类,要么PD.links
不是AssetLink
的枚举?
using System.Linq;
//...
foreach(var thisLink in PD.Links.OfType<AssetLink>())