简体   繁体   English

动态SQL到LINQ实体框架

[英]Dynamic SQL to LINQ Entity Framework

I have only the very basics of LINQ. 我只有LINQ的基础知识。 I speak SQL and JDBC, tasked with converting dynamic PL/SQL to LINQ Entity Framework. 我说的是SQL和JDBC,其任务是将动态PL / SQL转换为LINQ Entity Framework。 How can I add conditional WHERE clauses into LINQ queries? 如何在LINQ查询中添加条件WHERE子句? Here is a very simplified example (leaving out type info): 这是一个非常简化的示例(省略了类型信息):

Q1 := 'SELECT bDay  = b.birthday
            address = b.address
       FROM (' ;

Q2 := 'SELECT folks.birthday, Address.address
       FROM folks,
          (SELECT state,
                  surname AS name
           FROM Individuals, Addresses
           WHERE Individuals.addrId = Address.id
             AND Addresses.state = 'CA' ) find1
          ,(SELECT state,
                  surname AS name
           FROM Individuals, Addresses
           WHERE Individuals.addrId = Address.id
             AND Addresses.state = 'NV' ) find2
    AND find1.state(+) = folks.state';

IF input_parm = 'RED' THEN
   condAddOn :=condAddOn || 'AND folks.vacationHouse IS NOT NULL';
END IF;
IF input_parm = 'BLUE' THEN
   condAddOn :=condAddOn || 'AND folks.vacationHouse = 'Cabin';
END IF;
...
OPEN curs FOR Q1 || Q2 || condAddOn ')b';

Trying to figure out the C#/LINQ syntax, here is my attempt (working bottom up): 尝试弄清楚C#/ LINQ语法,这是我的尝试(自下而上):

var find1 = (from addr in Addresses
             from indiv in Individuals
             where indiv.addrId = addr.addrID
             select new
             {
                indiv.state,
                indiv.surname
             });

var find1OuterJoin = (from person in folks
                      join f1 in find1 on person.addrId equals f1.addrID
                      from f1OutJn in temp.DefaultIfEmpty()
                      select new
                      {
                         f1OutJn.state,
                         f1OutJn.surname
                      });

var Q2 = (from person in folks
          from addr in addresses
          from f1 in find1OuterJoin
          where person.addrId == addr.addrId
          && f1.state == folks.state
          select new
          {
            person.birthday
           ,addr.address
          });

var Q1 = (from q2 in Q1
          select new
          {bDay  = b.birthday
           ,address = b.address
         });

I don't know 1) if I introduced Q1 into the Q2 correctly 2) how to introduce the dynamic WHERE clauses to end up with an equivalent cursor statement: 我不知道1)我是否正确地将Q1引入了Q2 2)如何引入动态WHERE子句以等效的游标语句结束:

OPEN curs FOR Q1 || Q2 || condAddOn ')b';

Added: Can I use a functional or expression to include the dynamic bits? 补充:我可以使用函数或表达式来包含动态位吗? I saw a reference to Expression and Expandable(), but unsure. 我看到了对Expression和Expandable()的引用,但不确定。 Added: my attempt at the LINQ queries 补充:我对LINQ查询的尝试

When using the where method with link you are returned an IQueryable object. 在将where方法与链接一起使用时,将返回一个IQueryable对象。 This does not immediately execute the statement, therefore it is possible to do the following: 这不会立即执行该语句,因此可以执行以下操作:

var results = from person in folks
              join addr in addresses
              where person.addrId == addr.addrId
              select new { 
                  person.birthday, 
                  addr.address
                  };
 if(predicate){
    results = from r in results
               where /* new condition here */
               select r;
 }
 var resultSet = results.ToList().AsEnumerable();

for other link operators, especially when using lambda linq you can also use the AsQuerably extension method. 对于其他链接运算符,尤其是在使用lambda linq时,您也可以使用AsQuerably扩展方法。

such as, but not limited to: 例如但不限于:

var results = folks.join(address, 
                 person => person.addrId,
                 addr => addr.addrId
                 (person, addr) => new {
                                          person.birthday,
                                          addr.address
                                       }).AsQueryable();
if(predicate)
{ 
   results = results.where(/*predicate for the where condition*/);
}
var resultSet = results.ToList().AsEnumerable();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM