TargetException未处理

时间:2013-11-20 01:59:38

标签: c# linq entity-framework properties casting

我试图通过传入列名和值的字典来允许某人在单个表上运行查询。当我限制returnVersions时,它最初打破了,所以我输入currentvalue来测试它。即使在尝试将get转换为字符串时,我仍然会收到相同的错误。我不确定为什么会这样做。您将在下面找到代码,异常和堆栈跟踪。

代码:

    /// <summary>
    /// Runs a query on the version table based on a Dictionary you pass in
    /// </summary>
    /// <param name="queries">Dictonary... key = column name, value = value in column</param>
    /// <returns>List of MVersions matching all params, null if none</returns>
    public static List<MVersion> QueryVersions(Dictionary<string, object> queries)
    {
        if (queries.Count > 0)
        {
            IREntities context = new IREntities();

            var listOfProperties = typeof(version).GetProperties().ToList();

            List<MVersion> returnVersions = GetAllVersions();

            foreach (KeyValuePair<string, object> currentQuery in queries)
            {
                List<string> currentlistofstring = listOfProperties.Select(p => p.ToString()).ToList();
                var currentProperty = listOfProperties.Where(p => p.ToString().Split(' ').ElementAtOrDefault(1) == currentQuery.Key).FirstOrDefault();
                string currrentvalue = currentProperty.GetValue(returnVersions.ElementAtOrDefault(0), null).ToString();
                returnVersions = returnVersions.Where(v => currentProperty.GetValue(v).ToString() == currentQuery.Value.ToString()).ToList();
            }

            return returnVersions;
        }
        else
            return null;
    }

异常(无内部异常):

{"Object does not match target type."}

堆栈追踪:

   at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target)
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at IR.Model.Models.MVersion.QueryVersions(Dictionary`2 queries) in c:\Users\recursor\Documents\Visual Studio 2012\Projects\CS3280\Final Project\IR\IR.Model\Models\MVersion.cs:line 170
   at IR.Controller.CVersion.QueryVersions(Dictionary`2 queries) in c:\Users\recursor\Documents\Visual Studio 2012\Projects\CS3280\Final Project\IR\IR.Controller\CVersion.cs:line 125
   at ConsoleApplication2.Program.Main(String[] args) in c:\Users\recursor\Documents\Visual Studio 2012\Projects\CS3280\Final Project\IR\ConsoleApplication2\Program.cs:line 40
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

当前错误发生在字符串currentvalue行上。

编辑1: 错误在同一位置。我改变了split的调用,实际上有了char''。我仍然得到相同的错误,但是从堆栈跟踪以及调试器看起来它能够获得GetValue方法(currentProperty获取它看起来的实际System.string idversion属性),但它然后它摸索了对象何时出现。关于我接下来应该检查什么的任何想法?

1 个答案:

答案 0 :(得分:0)

当typeof需要成为模型时,typeof是一个实体。又名typeof(MVersion)