简体   繁体   English

在linq查询中用字符串连接枚举

[英]Concat a enum with a string in a linq query

I'm developing a WinForm application in c# with EF Code First Approach. 我正在使用EF代码优先方法在c#中开发WinForm应用程序。 The problem I have is when I do a linq query where a try to concat an Enum with a string. 我遇到的问题是当我执行linq查询时,尝试用字符串连接枚举。 The error is as follows: 错误如下:

Unable to cast the type 'Entities.VoucherType' to type 'System.Object'. 无法将类型“ Entities.VoucherType”转换为类型“ System.Object”。 LINQ to Entities only supports casting EDM primitive or enumeration types LINQ to Entities仅支持强制转换EDM基本类型或枚举类型

Then I show the Enum, POCO Entities and linq query: 然后显示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
        };

You could try this one: 您可以尝试以下一种方法:

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
           };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM