Search code examples
c#linqentity-framework-6.1

Concat a enum with a string in a linq query


I'm developing a WinForm application in c# with EF Code First Approach. The problem I have is when I do a linq query where a try to concat an Enum with a string. The error is as follows:

Unable to cast the type 'Entities.VoucherType' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types

Then I show the Enum, POCO Entities and linq query:

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

Solution

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