简体   繁体   中英

Select data from two tables with Linq/Lambda

When the customer logs in with Social Security Number to the website I want it to show the customers Accountname, Accountnumber and Balance. These information have to be fetched through database.

Accounts table:

   CREATE TABLE [dbo].[Accounts] (
[id]            BIGINT       IDENTITY (1, 1) NOT NULL,
[accountnumber] VARCHAR (50) NULL,
[accountname]   VARCHAR (50) NULL,
[SSN]           BIGINT       NOT NULL,
[CustomerId]    INT          NULL,
[balance]       VARCHAR (50) NULL,
[BalanceId]     INT          NULL,
[AccountId]     INT          NULL,
CONSTRAINT [PK_dbo.Accounts] PRIMARY KEY CLUSTERED ([id] ASC),
CONSTRAINT [FK_dbo.Accounts_dbo.Customer] FOREIGN KEY ([SSN]) REFERENCES [dbo].[Customer] ([SSN])
 );


GO
CREATE NONCLUSTERED INDEX [IX_SSN]
ON [dbo].[Accounts]([SSN] ASC);

Customer table:

    CREATE TABLE [dbo].[Customer] (
[id]                BIGINT          IDENTITY (1, 1) NOT NULL,
[Firstname]         VARCHAR (50)    NULL,
[Lastname]          VARCHAR (50)    NULL,
[SSN]               BIGINT          NOT NULL,
[Password]          VARBINARY (MAX) NULL,
[ConfirmPassword]   VARCHAR (MAX)   NULL,
[Salt]              VARCHAR (MAX)   NULL,
[AccountId]         INT             NULL,
[BalanceId]         INT             NULL,
[RegPayId]          INT             NULL,
[ConfirmedRegPayId] INT             NULL,
[CustomerId]        INT             NULL,
CONSTRAINT [PK_dbo.Customer] PRIMARY KEY CLUSTERED ([SSN] ASC)
   );

But this code shows me no customer:

       List<AccountsCTX> everyBalance = db.Accounts.Join(db.Customer, a => a.id, 
                                                                      c => c.SSN, 
                                                              (a, c) => new AccountsCTX()
        {
            //id = a.id,
            SSN = a.SSN,
            accountname = a.accountname,
            accountnumber = a.accountnumber,
            balance = a.balance
        }
                            ).ToList();
        return everyBalance;

I have "id" as the primary key from table "Accounts" and "SSN" as the foreign key from table "Customer".

I'm still freshman in programming so need more practice xD Let me know for more details and appreciate all the help I can get!

You can try

    public static AccountsCTX GetEveryBalance(long currentCustomerSsn)
    { 
     AccountsCTX everyBalance = db.Accounts.Join(db.Customer, a => a.SSN, 
                                                                          c => c.SSN, 
                                                                  (a, c) => new AccountsCTX()
            {
                //id = a.id,
                SSN = a.SSN,
                accountname = a.accountname,
                accountnumber = a.accountnumber,
                balance = a.balance
            }
                                ).Where(x=>x.SSN==currentCustomerSsn).FirstOrDefault();//currentCustomerSsn is enter ssn by Customer
            return everyBalance;
    }

    //Create customer
    public long insertCusReg(CustomerCTX inCusReg)
    {
        ...
        try
        {
            ...
            return inCusReg.SSN;
        }
        catch (Exception)
        {
            return 0;
        }


    } 

Call Method

currentCustomerSsn=insertCusReg(CustomerCTX);// pass CustomerCTX object 
AccountsCTX everyBalance=GetEveryBalance(currentCustomerSsn);

actually i dont see any relation between the two tables provided except SSN, this inner join will fetch you all the records which has same SSN id in both the tables:

List<AccountsCTX> everyBalance = db.Accounts.Join(db.Customer, a => a.SSN, 
                                                                      c => c.SSN, 
                                                              (a, c) => new AccountsCTX()
        {
            SSN = a.SSN,
            accountname = a.accountname,
            accountnumber = a.accountnumber,
            balance = a.balance
        }).Where(x=>x.SSN==InputSSN).ToList();
        return everyBalance;

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