Home > Blockchain >  Entity Framework Core: join two tables and get the properties I want using LINQ
Entity Framework Core: join two tables and get the properties I want using LINQ

Time:06-05

I have two related tables. Then I use LINQ to query Data. this is my code

var items = await (from a in queryable
                   join b in _context.TUserGrant on a.UserNo equals b.UserNo
                   join c in _context.TProviderInfo on a.ProviderNo equals c.ProviderNo
                   orderby a.BillNo
                   select new
                          {
                              a.BillNo,
                              a.NotificeBillNo,
                              makeName = b.UserName,
                              a.MakeDate,
                              a.ProviderNo,
                              c.ProviderName,
                              a.CheckTime,
                              a.CheckAddress,
                              a.CheckName,
                              a.StatusTitle,
                          }).ToListAsync();

My problem is that I need all the columns of the first table, which is all the values of A.

I also need some columns from table B.

I wonder if there is an easy way to get these columns.

Instead of setting them one by one in the SELECT method.

CodePudding user response:

You can try this

var items = await (from a in queryable
               join b in _context.TUserGrant on a.UserNo equals b.UserNo
               join c in _context.TProviderInfo on a.ProviderNo equals c.ProviderNo
               orderby a.BillNo
               select new
                      {
                          tabA = a,                              
                          makeName = b.UserName
                      }).ToListAsync();
  • Related