简体   繁体   中英

Linq query with multiple joins

I am using Entity Framework to work with my DB and I need help forming a LINQ query that will help me to get columns LoginId and AccNumber. I need only 1 LoginId for every single AccNumber, doesn't matter which LoginId it is. As it stands now, there are about 800K LoginIds and only ~3000 AccNumber.

Therefore, at the end I need 1 AccNumber associated with 1 LoginId. So I should have ~3000 rows and 2 columns.

Here are the tables that I need joined:

Item

ItemId, AccNumber, other irrelevant columns

Block_Item

ItemId, BlockId, other irrelevant columns

Bookversion_Block

BookversionId, BlockId, other irrelevant columns

Sudent

LoginId, BookversionId, other irrelevant columns


Ideally, I want to replicate this SQL query using LINQ (just trying to provide as much info as possible)

select max(StudentId) as StudentId, BookletVersionId into #ST from Student
group by BookletVersionId

select #ST.BookletVersionId, LoginId into #T1 from #ST
join Student ST on #ST.StudentId = ST.StudentId

select BookletVersionId, BlockId into #BVB from BookletVersion_Block
where
                BookletVersionId in (select BookletVersionId from #ST)

select #T1.BookletVersionId, #BVB.BlockId, LoginId into #T2 from #T1
join BookletVersion_Block #BVB on #T1.BookletVersionId = #BVB.BookletVersionId

select max(BlockId) as BlockId, ItemId into #BI from Block_Item
where
                BlockId in (select BlockId from #T2)
group by ItemId

select BookletVersionId, #T2.BlockId, ItemId, LoginId into #T3 from #T2
join #BI on #T2.BlockId = #BI.BlockId

select max(LoginId) as LoginId, AccessionNumber from #T3
join Item I on #T3.ItemId = I.ItemId
group by AccessionNumber order by LoginId

Here is what I tried, however, the results that I get back are not correct and I get back like 183,000 records. Obviously because my LINQ query isn't correct, which is why I am asking for help.

var q = (from items in context.Items
         join context.Block_Item
         on items.ItemId equals bi.ItemId into bi
         join context.BookletVersion_Block
         on bi.Select(x => x.BlockId).FirstOrDefault() equals BVB.BlockId into BVB
         join context.Students
         on BVB.Select(x => x.BookletVersionId).FirstOrDefault() equals st.BookletVersionId into st
          //'VH098334'
          select new { LoginId = st.Select(x => x.LoginId).FirstOrDefault().ToString(), AccNum = items.AccessionNumber.ToString() });

You can use a sub-query along with FirstOrDefault inside your select statement in order to get the LoginId

var query =
   from items in context.Items
   select new 
   {
      AccNum = items.AccessionNumber,
      LoginId = (
         from bi in context.Block_Item  
         join bb in context.BookletVersion_Block on bi.BlockId equals bb.BlockId
         join st in context.Students on bb.BookversionId equals st.BookversionId
         where items.ItemId == bi.ItemId
         select st.LoginId
      ).FirstOrDefault()
   };

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