Linq查询返回匿名类型错误

时间:2017-12-21 04:14:34

标签: entity-framework linq wcf

我正在尝试开发银行系统。我试图将三个表记录连接到单个表中。但问题是当我编译它时我得到了以下错误。

  

错误CS1929'IOrderedQueryable<>'不含   'Concat'的定义和最佳扩展方法重载   'ParallelEnumerable.Concat<>(ParallelQuery<>,IEnumerable<>)'需要一个   “ParallelQuery<>”类型的接收器

这是我的Linq查询。

  public string TranscationDetails(string Account_Number)
        {

            var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse
            using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
            {

    var inOut = context.Current_Account_Deposit.Where(x => x.Account_Number == accountNumber).Select(w => new
            {
                w.Account_Number,
                Deposit = (decimal?)null,
                Withdrawal = (decimal?)w.Amount,
                w.Date
            }).Concat(context.Current_Account_Withdraw.Select(d => new
            {
                d.Account_Number,
                Deposit = (decimal?)d.Amount,
                Withdrawal = (decimal?)null,
                d.Date
            })).OrderBy(r => r.Date)
         .Concat(context.Current_Account_Details.Select(e => new
         {
             //You should perform same anonymous type which you want to concat
             Account_Number = e.Account_Number,
             Deposit = (decimal?)e.Account_Balance,
             Withdrawal = (decimal?)null,
            e.Account_Fees
         }));


                var js = new System.Web.Script.Serialization.JavaScriptSerializer();

                return js.Serialize(inOut); // return JSON string
            }
        }
    }


This is DBContext class.

 public partial class HalifaxDatabaseEntities : DbContext
    {
        public HalifaxDatabaseEntities()
            : base("name=HalifaxDatabaseEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Web_User_login> Web_User_login { get; set; }
        public virtual DbSet<USER> USERS { get; set; }
        public virtual DbSet<tblUser> tblUsers { get; set; }
        public virtual DbSet<Current_Account_Holder_Details> Current_Account_Holder_Details { get; set; }
        public virtual DbSet<Current_Account_Details> Current_Account_Details { get; set; }
        public virtual DbSet<Current_Account_Deposit> Current_Account_Deposit { get; set; }
        public virtual DbSet<Current_Account_Withdraw> Current_Account_Withdraw { get; set; }
    }

这是Model Class .. enter image description here

以下是我预期的结果,当我点击帐号时,它应显示特定记录enter image description here

2 个答案:

答案 0 :(得分:2)

错误很清楚。您正尝试Concat个不同的匿名类型。

var inOut = context.Current_Account_Deposit.Select(w => new
    {
        Account_Number = w.Account_Number
        ,
        Deposit = (decimal?)null,

        Withdrawal = (decimal?)w.Amount
        ,
        Date = w.Date
    }).Concat(context.Current_Account_Withdraw.Select(d => new
    {
        Account_Number = d.Account_Number
        ,
        Deposit = (decimal?)d.Amount
        ,
        Withdrawal = (decimal?)null
        ,
        Date = d.Date
    })).OrderBy(r => r.Date)
    .Concat(context.Current_Account_Details.Select(e => new
    {
        //You should perform same anonymous type which you want to concat
        Account_Number = e.Account_Number,
        Deposit = (decimal?)e.Account_Balance,
        Withdrawal = (decimal?)null,
        Date = null
    }));

此外,我不了解要求和详细信息,但您确定使用Concat吗?您似乎需要创建join而不是Concat。如果实体具有导航属性,我强烈建议您使用它们。

答案 1 :(得分:1)

修改代码,为Concat的每个参与者创建与输出相同的模式/类,这里因为两个匿名类型永远不会相同,因此它总是会失败,Concat需要相同/一致输入T,查看下方的详细信息:

对于Concat调用,

IEnumerable<T>需要IEnumerable<T>,因此类型T保持一致,这对于匿名类型是不可能的,请检查定义here,如:

public static IEnumerable<TSource> Concat<TSource>( this IEnumerable<TSource> first, IEnumerable<TSource> second )

class Account
{
   public int? Account_Number {get;set;}

   public decimal? Deposit {get;set;}

   public decimal? Withdrawal {get;set;}

   public decimal? Account_Balance {get;set;}

   public DateTime? Date {get;set;}
}

public string TranscationDetails(string Account_Number)
{

    var accountNumber = int.Parse(Account_Number);//It could be better to use TryParse
    using (HalifaxDatabaseEntities context = new HalifaxDatabaseEntities())
    {

        var inOut = context.Current_Account_Deposit.Select(w => new Account
        {
            Account_Number = w.Account_Number
,
            Deposit = (decimal?)null,

            Withdrawal = (decimal?)w.Amount
,
            Date  = w.Date
        }).Concat(context.Current_Account_Withdraw.Select(d => new Account
        {
            Account_Number = d.Account_Number
        ,
            Deposit = (decimal?)d.Amount
        ,
            Withdrawal = (decimal?)null
        ,
            Date = d.Date
        })).OrderBy(r => r.Date)
        .Concat(context.Current_Account_Details.Select(e => new Account
        {
            Account_Number = e.Account_Number,
            Account_Balance = (decimal?)e.Account_Balance

        }));


        var js = new System.Web.Script.Serialization.JavaScriptSerializer();

        return js.Serialize(inOut); // return JSON string
    }
}