【问题标题】:Concat a enum with a string in a linq query在 linq 查询中将枚举与字符串连接起来
【发布时间】:2014-06-19 15:07:19
【问题描述】:

我正在使用 EF 代码优先方法在 c# 中开发 WinForm 应用程序。我遇到的问题是当我执行 linq 查询时,尝试将 Enum 与字符串连接起来。错误如下:

无法将类型“Entities.VoucherType”转换为类型“System.Object”。 LINQ to Entities 仅支持转换 EDM 基元或枚举类型

然后我显示 Enum、POCO 实体和 linq 查询:

public enum VoucherType
{
    FAC = 1,
    BV,
    TKT,
    GR
}
public partial class Order
{
    public int OrderId { get; set; }
    public VoucherType VoucherType { get; set; }
    public string VoucherSeries { get; set; }
    public string VoucherNumber { get; set; }
}
public partial class Income
{
    public int IncomeId { get; set; }
    public int OrderId { get; set; }
    public decimal IncomeAmount { get; set; }
}
var q = from income in context.Incomes
        join order in context.Orders on income.OrderId equals order.OrderId
        select new
        {
            Voucher = order.VoucherType + "-" + order.VoucherSeries + "-" + order.VoucherNumber,
            Amount = income.IncomeAmount
        };

【问题讨论】:

  • Entity Framework 4 不支持枚举。 EF5 添加了此支持。
  • @SamLeach 我正在使用 EF6.1
  • 好的,你已经标记了 version4。我已将其更改为版本 6。

标签: c# linq entity-framework-6.1


【解决方案1】:

你可以试试这个:

var q = (from income in context.Incomes
         join order in context.Orders 
         on income.OrderId equals order.OrderId
         select new 
         {
             VoucherType = order.VoucherType,
             VoucherSeries = order.VoucherSeries,
             VoucherNumber = order.VoucherNumber,
             IncomeAmount = income.IncomeAmout
         }).AsEnumerable()
           .Select(x=> new
           {
               Voucher = x.VoucherType + "-" + x.VoucherSeries + "-" +x.VoucherNumber,
               Amount = x.IncomeAmount
           };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-13
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多