Home > Software design >  C# How to access fields tables in joint tables in linq query?
C# How to access fields tables in joint tables in linq query?

Time:07-06

Supposing i have 3 tables in the below picture that joined together. how can i access 'Email' field in 'Users' table in my query on 'UserReferralJoins' table ?

can any one help me?

var referalId = await _dbContext.UserReferrals.Where(x => x.UserId == CurrentUserId).Select(x => x.UserReferralId).FirstOrDefaultAsync();
        var list = await _dbContext.UserReferralJoins
            .Where(i => i.UserReferralId == referalId)
            .Select(x => new
            {
                Id = x.UserReferralJoinId,
                UserEmail = ???
                x.JoinDate
            }).ToListAsync(cancellationToken);

enter image description here

CodePudding user response:

Use this pattern in your select query

 UserEmail =x.UserReferral.ReferralCode

CodePudding user response:

The name of field in your table is very important I see that your join field name is correct in your database as I see in picture very simply you can use linq query for fetching your purpose for example use 'userreferral'. user and fetch the field of user table. if you want more help plz comment me.

  • Related