实体框架:LINQ to Entities只支持转换Entity Data Model原始类型 [英] Entity Framework: LINQ to Entities only supports casting Entity Data Model primitive types

查看:123
本文介绍了实体框架:LINQ to Entities只支持转换Entity Data Model原始类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个方法来允许一个表达式被传递给orderby子句,但是我遇到这个问题。

I wrote a method to allow for an Expression to be passed in for the orderby clause, but I ran into this problem.


无法将类型
'System.DateTime'转换为
'System.IComparable'。 LINQ to Entities
只支持转换Entity Data
模型原始类型。

Unable to cast the type 'System.DateTime' to type 'System.IComparable'. LINQ to Entities only supports casting Entity Data Model primitive types.

基本上表达式是这样的: / p>

Basically the expression is this:

Expression<Func<K, IComparable>> orderBy

这样使用:

SomeEntities.SomeTable
.Where
(
   whereClause
)
.Select
(
   selectClause
)
.OrderBy(orderBy)

这个想法是这样的,使用字典来保持字符串匹配,如:

The idea is so that I can use a dictionary to hold string matches to expressions like:

_possibleSortForForumItem.Add("CreateDate", item => item.CreateDate);

然后我有一个方法接收排序字符串,并返回表达式,如果它匹配一个键字典,如果不返回一些默认。 (这个想法是一种控制它可以排序的方法)现在这对于String属性有效,但是到目前为止,对于datetime或整数,我收到上面的错误信息。

Then I have a method that takes in the sort string and returns the expression if it matches a key in the dictionary, if not returns some default. (The idea being a way to control what it can be ordered by) Now this works for String properties, but so far not for datetime or integer as I get the error message above.

现在,我(松散地)了解问题是,实体框架需要它是一个主/ EDM类型,因为它必须将C#DateTime转换成数据库可以处理。

Now far as I (loosely) understand the problem is that Entity Framework needs it to be a Primary/EDM type because it has to convert the C# DateTime into something the database can handle.

有没有办法将datetime转换为原始类型,这样仍然可以工作?

Is there a way to convert the datetime to a primitive type so that this will still work?

解决方案

通过方法获取顺序的方法:(接受查询并以有序格式返回) p>

The method for getting the order by method: (Take in a query and return it in "ordered form")

private static Func<IQueryable<ForumViewItem>, IOrderedQueryable<ForumViewItem>> GetMethodForSort(String sortBy)
{
  if (_methodForSort == null)
  {
    _methodForSort = new Dictionary<String, Func<IQueryable<ForumViewItem>, IOrderedQueryable<ForumViewItem>>>();
    _methodForSort.Add(SortForumViewItemCreatedOn, item => item.OrderBy(innerItem => innerItem.CreatedOn));
    ...
  }

  Func<IQueryable<ForumViewItem>, IOrderedQueryable<ForumViewItem>> orderMethod;

  if(String.IsNullOrEmpty(sortBy) || !_methodForSort.ContainsKey(sortBy))
  {
    orderMethod = _methodForSort["ForumName"];
  }
  else
  {
    orderMethod = _methodForSort[sortBy];
  }

  return orderMethod;
}

通用查询方法的方法签名:

The method signature for the generic query method:

IList<K> GetListForGrid<T, K>(this ObjectQuery<T> query, ... Func<IQueryable<K>, IOrderedQueryable<K>> orderBy, ...)

使用传入方法:

initialQuery = query
  .Where
  (
    somethingEqualsSomething
  )
  .Select
  (
    selectClause
  );

var orderedQuery = orderBy(initialQuery);

returnValue = orderedQuery
  .Skip(numberToShow * realPage)
  .Take(numberToShow)
  .ToList();


推荐答案

实体框架使这很困难,我不确定有一种方法可以使用单个返回值类型(IComparable,object等)来执行所需操作。您可能会考虑将您的设计重新编成一个名称为 - Func< IQueryable>,IOrderedQueryable< K> 值的字典:

The Entity Framework makes this difficult and I'm not sure there's a way to do what you want to do with a single return value type (IComparable, object, etc). You might consider reworking your design into a dictionary of name-to-Func<IQueryable<K>, IOrderedQueryable<K>> values:

_possibleSortForForumItem.Add("CreateDate", 
    query => query.OrderBy(item.CreateDate));

然后应用如下:

var orderedQuery = query.OrderBy(item => item.DefaultOrderColumn);

Func<IQueryable<K>, IOrderedQueryable<K>> assignOrderBy = null;

if (_possibleSortForForumItem.TryGetValue(orderColumnName, out assignOrderBy))
{
    orderedQuery = assignOrderBy(query);
}

这篇关于实体框架:LINQ to Entities只支持转换Entity Data Model原始类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆