C# 带计算列的System.Linq.Expressions

C# 带计算列的System.Linq.Expressions,c#,asp.net-mvc,entity-framework,linq,kendo-asp.net-mvc,C#,Asp.net Mvc,Entity Framework,Linq,Kendo Asp.net Mvc,在我正在进行的MVC项目中,我有一个剑道下拉列表,它调用我的服务器操作来执行任何读取操作(包括过滤): 小部件绑定到2个属性: IdServiceRequest(值)和idServiceRequestDescriptione(文本) IdServiceRequest是实体Id,idServiceRequestDescriptione是我在实体的分部类中定义的计算列 public string IdServiceRequestDescrizione { ge

在我正在进行的MVC项目中,我有一个剑道下拉列表,它调用我的服务器操作来执行任何读取操作(包括过滤):

小部件绑定到2个属性: IdServiceRequest(值)和idServiceRequestDescriptione(文本)

IdServiceRequest是实体Id,idServiceRequestDescriptione是我在实体的分部类中定义的计算列

public string IdServiceRequestDescrizione
        {
            get
            {
                if (TipologiaChiamata.IdTipologiaChiamata != (int)Enums.TipologiaChiamata.Automatica)
                {
                    return IdServiceRequest + " " + Note;
                }
                else
                {
                    return IdServiceRequest + " " + Email.EmailOggetto;
                }
            }
        }
为了创建正确的linq查询以执行筛选器,我创建了一个System.linq.Expression,该表达式将我的实体转换为包含2个字符串属性的DTO:

Expression<Func<ServiceRequest, DTO>> funcError = s => new DTO
            {
                IdServiceRequest = s.IdServiceRequest,
                IdServiceRequestDescrizione = s.IdServiceRequestDescrizione
            };
当我使用ToList()具体化查询时,会发生以下错误:

如果我不是在部分中定义idServiceRequestDescriptione,而是在System.Linq.Expression中定义它,则不会抛出错误,一切正常

Expression<Func<ServiceRequest, DTO>> func = s => new DTO
            {
                IdServiceRequest = s.IdServiceRequest,
                IdServiceRequestDescrizione = s.TipologiaChiamata.IdTipologiaChiamata != (int)EntityModel.Enums.TipologiaChiamata.Automatica ? s.IdServiceRequest + " " + s.Note : s.IdServiceRequest + " " + s.Email.EmailOggetto
            };
Expression func=s=>new DTO
{
IdServiceRequest=s.IdServiceRequest,
idServiceRequestDescriptione=s.tipologiamata.idtipologiamata!=(int)EntityModel.Enums.tipologiamata.Automatica?s.IdServiceRequest+“”+s。注意:s.IdServiceRequest+“”+s.Email.EmailOggetto
};

有人能给我一个关于这种行为的专业解释吗?为什么我不能在linq表达式中引用自定义属性?

计算列是用C#实现的,但EF仅在数据库模型上操作(转换为SQL或从SQL转换而来)

如果将方法调用的顺序颠倒到

.ToList().Select(funcError);

i、 e.首先将记录转换为C#entity对象,然后可以访问计算列。(我通常使用NHibernate,但问题是相同的)

有一些选择:

1) 林克变换

这样你就可以写这样的东西:

public class Customer
{
  public string FirstName { get; set; }
  public string LastName { get; set; }

  private static readonly CompiledExpression<Customer, string> fullNameExpression
     = DefaultTranslationOf<Customer>.Property(e => e.FullName).Is(e => e.FirstName + " " + e.LastName);

  [NotMapped]
  public string FullName
  {
    get { return fullNameExpression.Evaluate(this); }
  }
}

var customers = ctx.Customers
  .Select(c => new
  {
    FullName = c.FullName
  })
  .WithTranslations();

linq.Expression不是也用c#实现的吗?在我的场景中,我无法具体化该指令中的实体,因为额外的过滤器应用于Iqueryable对象。需要说明的是:在投影到DTO之后必须使用ToList()。这个问题的问题基本上是相同的:
.ToList().Select(funcError);
public class Customer
{
  public string FirstName { get; set; }
  public string LastName { get; set; }

  private static readonly CompiledExpression<Customer, string> fullNameExpression
     = DefaultTranslationOf<Customer>.Property(e => e.FullName).Is(e => e.FirstName + " " + e.LastName);

  [NotMapped]
  public string FullName
  {
    get { return fullNameExpression.Evaluate(this); }
  }
}

var customers = ctx.Customers
  .Select(c => new
  {
    FullName = c.FullName
  })
  .WithTranslations();
public class Customer
{
  public string FirstName { get; set; }
  public string LastName { get; set; }

  [NotMapped]
  [Computed]
  public string FullName
  {
    get { return FirstName + " " + LastName; }
  }
}
var customers = ctx.Customers
  .Select(c => new
  {
    FullName = c.FullName
  })
  .Decompile();