1

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
        };
3
  • Entity Framework 4 does not support enums. EF5 added this support. Commented Jun 19, 2014 at 15:12
  • @SamLeach I am using EF6.1 Commented Jun 19, 2014 at 15:35
  • Okay, you had version4 tagged. I've changed it to version6. Commented Jun 20, 2014 at 8:20

1 Answer 1

1

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
           };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.