簡體   English   中英

在linq查詢中用字符串連接枚舉

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

我正在使用EF代碼優先方法在c#中開發WinForm應用程序。 我遇到的問題是當我執行linq查詢時,嘗試用字符串連接枚舉。 錯誤如下:

無法將類型“ 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
        };

您可以嘗試以下一種方法:

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