Join more table using with LINQ in C#

Viewed 54

I have a problem joining 1 more table using with LINQ in C#. Now my query can join 1 table.My existing query is TnWhatsapp table join Tenant table Below is my coding:

var query = _saasdbContext.TnWhatsapp.AsNoTracking();

if (isSAASAdmin == false)
    query = query.Where(x => x.TenantId == tenantID);

int totalCount = await query.CountAsync();
query = QueryableExtensions.QueryOrderBy(query, sortList);
List<TnWhatsappMessage> whatsappDedicatedListForTenant = new List<TnWhatsappMessage>();

if (take == 0)
    whatsappDedicatedListForTenant = await query.Skip(skip)
                                    .Join(_saasdbContext.Tenant.AsNoTracking(),
                                    tnWhatsapp => tnWhatsapp.TenantId,
                                    tenant => tenant.Id,
                                    //(tnWhatsapp, tenant) => new { tnWhatsapp, tenant })
                                    //.Join(_saasdbContext.TnBranch.AsNoTracking(),
                                    //branch => branch.Id,
                                    (tnWhatsapp, tenant) => new TnWhatsappMessage()
                                    {
                                        Id = tnWhatsapp.Id,
                                        TenantId = tnWhatsapp.TenantId,
                                        BranchId = tnWhatsapp.BranchId,
                                        InstanceId = tnWhatsapp.InstanceId,
                                        Token = tnWhatsapp.Token,
                                        TenantName = tenant.CompanyName,
                                        DayLimit = tnWhatsapp.DayLimit,
                                        MinuteLimit = tnWhatsapp.MinuteLimit,
                                    })
                                    .ToListAsync();

But now I am not sure how to join another table, I want this TnWhatsapp table BranchId to join TnBranchtable Id, then I want to get TnBranch table Code. Below is my edit part code inside the query, but it cannot work.

var query = _saasdbContext.TnWhatsapp.AsNoTracking();

                if (isSAASAdmin == false)
                    query = query.Where(x => x.TenantId == tenantID);

                int totalCount = await query.CountAsync();
                query = QueryableExtensions.QueryOrderBy(query, sortList);
                List<TnWhatsappMessage> whatsappDedicatedListForTenant = new List<TnWhatsappMessage>();

                if (take == 0)
                    whatsappDedicatedListForTenant = await query.Skip(skip)
                                                    .Join(_saasdbContext.Tenant.AsNoTracking(),
                                                    tnWhatsapp => tnWhatsapp.TenantId,
                                                    tenant => tenant.Id,
                                                    (tnWhatsapp, tenant) => new { tnWhatsapp, tenant })
                                                    .Join(_saasdbContext.TnBranch.AsNoTracking(),
                                                    tnBranch => tnBranch.Id,
                                                    tnWhatsapp => tnWhatsapp.BranchId,
                                                    (tnWhatsapp, tenant, tnBranch) => new TnWhatsappMessage()
                                                    {
                                                        Id = tnWhatsapp.Id,
                                                        TenantId = tnWhatsapp.TenantId,
                                                        BranchId = tnWhatsapp.BranchId,
                                                        InstanceId = tnWhatsapp.InstanceId,
                                                        Token = tnWhatsapp.Token,
                                                        TenantName = tenant.CompanyName,
                                                        BranchName = tnBranch.Code,
                                                        DayLimit = tnWhatsapp.DayLimit,
                                                        MinuteLimit = tnWhatsapp.MinuteLimit,
                                                    })
                                                    .ToListAsync();

My error messages like the below picture:

enter image description here

This picture is my last try, but it also shows the errors.

pic1

Hope someone can guide me on how to solve this problem. Thanks.

1 Answers

I have enhanced the comment what I provided with the suggestion by @madreflection. Here is the Query syntax of your LINQ.

whatsappDedicatedListForTenant = await (
    from tnWhatsapp in query.Skip(skip)
    join tenant in _saasdbContext.Tenant.AsNoTracking() on tnWhatsapp.TenantId equals tenant.Id
    join tnBranch in _saasdbContext.TnBranch.AsNoTracking() on tnWhatsapp.BranchId equals tnBranch.Id
    select new TnWhatsappMessage()
    {
        Id = tnWhatsapp.Id,
        TenantId = tnWhatsapp.TenantId,
        BranchId = tnWhatsapp.BranchId,
        InstanceId = tnWhatsapp.InstanceId,
        Token = tnWhatsapp.Token,
        TenantName = tenant.CompanyName,
        BranchName = tnBranch.Code,
        DayLimit = tnWhatsapp.DayLimit,
        MinuteLimit = tnWhatsapp.MinuteLimit,
    }
).ToListAsync();

Hope this helps!

EDIT : Modified per the source of the OP.

Related