Linq查询返回记录列表而不是显示单个记录

时间:2017-12-21 02:10:04

标签: c# sql-server linq

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

  

错误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
                }
            }
        }

这是DBContext类。

  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

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

3 个答案:

答案 0 :(得分:2)

你只能在本地使用Concat。尝试在每个Concat之前添加.ToList(),以便在连接之前在本地实现记录。

注意:您的Where子句仅适用于Current_Account_Deposit。 Withdraw和Details将返回所有记录。

尝试生成3个基本上下文查询。每个表一个,并使用ToList()完成每个表。稍后使用Concat将所有3个列表连接成一个列表。

这会让你理解这个概念,你可以从这里继续前进。

var q1 = 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
    }).ToList();

    var q2 = context.Current_Account_Withdraw.Select(d => new
    {
        d.Account_Number,
        Deposit = (decimal?)d.Amount,
        Withdrawal = (decimal?)null,
        d.Date
    }).OrderBy(r => r.Date).ToList();

    var q3 = 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
         }).ToList();

    var inOut = q1.Concat(q2).Concat(q3).ToList();

您可能需要将相同的Where子句添加到q2和Q3。我不确定你是否想要那个。

答案 1 :(得分:1)

假设您已经有记录列表。

应该是这样的。

var getList = inOut.Where(x=>x.Account_Number == 
Account_Number).FirstOrDefault();

如果您的inOut是列表的真实结果。

答案 2 :(得分:1)

我在select .Where(x=> x.Account_Number == accountNumber)

之前添加了where子句

改变这个:

 var inOut = context.Current_Account_Deposit.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
            {
                e.Account_Number,
                Account_Balance=(decimal?)e.Account_Balance
            }));

致:

 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
            {
                e.Account_Number,
                Account_Balance=(decimal?)e.Account_Balance
            }));

因为您需要在执行concat之前获取特定的accountnumber详细信息