Concat在linq查询中带有字符串的枚举

时间:2014-06-19 15:07:19

标签: c# linq entity-framework-6.1

我正在使用EF Code First Approach在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
        };

1 个答案:

答案 0 :(得分: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
           };