简体   繁体   中英

Linq Query Return List of Records instead of displaying single record

I trying to developing banking system . I trying to join three tables records into single table . but the problem is when i compile it i got following errors.

Error CS1929 'IOrderedQueryable<>' does not contain a definition for 'Concat' and the best extension method overload 'ParallelEnumerable.Concat<>(ParallelQuery<>, IEnumerable<>)' requires a receiver of type 'ParallelQuery<>

Here is my Linq Query .

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

Here is the Model Class .. 在此输入图像描述 Here is the Database record 在此输入图像描述

Here is the Result i Expect when i clicked the account number its should display the specific record 在此输入图像描述

You can only Concat locally. Try adding a .ToList() before each Concat so it materialize the records locally before concatenating.

Note: your Where clause is only applying to Current_Account_Deposit. Withdraw and Details will return all records.

Try to generate 3 basic context queries. One for each table and finish each one with ToList(). Later concatenate all 3 lists into one list using Concat.

This will make you understand the concept and you can move forward from here.

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();

You may need to add the same Where clause to q2 and Q3. I wasn't sure if you wanted that.

Assumming that you have already the list of records.

It should look like.

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

if your inOut is a true result of a list.

I have added where clause before select .Where(x=> x.Account_Number == accountNumber)

Change this :

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

To :

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

As You need to get the specific accountnumber details before performing concat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM