Rewriting a SQL with LINQ

本文关键字:LINQ with SQL Rewriting | 更新日期: 2023-09-27 18:16:27

我有这个SQL:

select * from ADDRESSES where ZIPCODE ='66210'and ADDRESS_K in 
       (select ADDRESS_K from GROUPADDRESS where GROUP_K in 
       (select GROUP_K from GROUPS where NPI = 'groupnpi' and TAXID = 'grouptin') 
        and ADDRESSTYPE_RTK = '_REI0PVM65')

我想把它写成LINQ

有人可以帮助转换成LINQ ?我只能做到这些了

Rewriting a SQL with LINQ

这没有经过测试,但应该给你一个好的开始(这有助于你如何处理LINQ to SQL的IN子查询?)

var innerInnerQuery = from g in GROUPS
                      where NPI == "roupnpi" 
                          && TAXID == "grouptin"
                          && ADDRESSTYPE_RTK == "_REI0PVM65"
                      select g.GROUP_K;
var innerQuery = from ga in GROUPADDRESS
                 where innerInnerQuery.Contains(ga.GROUP_K)
                 select ga.ADDRESS_K;
var query = from a in ADDRESSES
            where ZIPCODE == "66210"
                && innerQuery.Contains(a.ADDRESS_K)
            select a;

这是我能想到的…请让我知道它是否有效

var innerQuery = from Group_K in GroupAddress
                  where Group_K ==
                         (
                              from iq in Groups
                              where iq.NPI.Contains("groupnpi")
                              where iq.TAXID == "grouptin"
                              where iq.ADDRESSTYPE_RTK == "REI0PVM65"
                              select iq.NPI
                         )
                         select Group_K.Address_K;
        var result = from a in addresses
                     where a.ZipCode.Contains("groupnpi")
                     where a == innerQuery
                     select a;