【问题标题】:MVCCrud Using LinqToEntitiesMVCCrud 使用 LinqToEntities
【发布时间】:2012-05-19 22:09:13
【问题描述】:

有一个名为 MVCCrud 的示例应用程序。这个例子非常好,我想将它用作我正在从事的项目的框架。

问题是 MVCCrud 使用 LingToSQL,我想使用 LinqToEntities。转换到 LinqToEntities 后,除了一个地方,我几乎所有东西都能正常工作。

在下面的代码中 i = typeof(TModel).GetProperty(primaryKey).GetValue(p, null), 细胞 = 获取细胞(p) 它给出了一个 Linq to Entities 无法识别 GetValue。

有人可以帮我重构以下代码吗?

            items = items.OrderBy(string.Format("{0} {1}", sidx, sord)).Skip(pageIndex * pageSize).Take(pageSize).AsQueryable();

            // Generate JSON
            var jsonData =
                new
                    {
                        total = totalPages,
                        page,
                        records = totalRecords,
                        rows = items.Select(
                            p => new
                                {
                                    // id column from repository
                                    i = typeof(TModel).GetProperty(primaryKey).GetValue(p, null),
                                    cell = getCells(p)
                                }).ToArray()
                    };
            return Json(jsonData);

这里是 getCell 方法:

    private string[] getCells(TModel p)
    {
        List<string> result = new List<string>();

        string a = actionCell(p);
        if (a != null)
        {
            result.Add(a);
        }

        foreach (string column in data_rows.Select(r => r.value))
        {
            try
            {
                // hack for tblcategory.name
                string[] parts = column.Split('.');

                // Set first part
                PropertyInfo c = typeof(TModel).GetProperty(parts[0]);
                object tmp = c.GetValue(p, null);

                // loop through if there is more than one depth to the . eg tblCategory.name
                for (int j = 1; j < parts.Length; j++)
                {
                    c = tmp.GetType().GetProperty(parts[j]);
                    tmp = c.GetValue(tmp, null);
                }

                if (tmp.GetType() == typeof(DateTime))
                {
                    result.Add(((DateTime)tmp).ToString(dateTimeFormat));
                }
                else if (tmp.GetType() == typeof(float))
                {
                    result.Add(((float)tmp).ToString(decimalFormat));
                }
                else if (tmp.GetType() == typeof(double))
                {
                    result.Add(((double)tmp).ToString(decimalFormat));
                }
                else if (tmp.GetType() == typeof(decimal))
                {
                    result.Add(((decimal)tmp).ToString(decimalFormat));
                }
                else
                {
                    result.Add(tmp.ToString());
                }
            }
            catch (Exception)
            {
                result.Add(string.Empty);
            }
        }

        return result.ToArray();
    }

【问题讨论】:

    标签: c# entity-framework linq-to-entities


    【解决方案1】:

    这样做ToList() 而不是AsQueryable()

    items = items.OrderBy(string.Format("{0} {1}", sidx, sord)).Skip(pageIndex * pageSize).Take(pageSize).ToList();
    

    您不能在“内部”linq 查询中执行任何外部方法。

    您是否可以说它在 Linq2Sql 中工作,那么您应该知道何时调用任何外部方法“像 ToString()”Linq2Sql 将从数据库中获取所有数据,然后在内存中处理您的查询,如果您这样做可能会造成严重伤害有很多记录。

    更多信息请查看this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多