1. 程式人生 > >SQL to LinQ 之查詢條件 In的表示(.any)

SQL to LinQ 之查詢條件 In的表示(.any)

 strQuery = "SELECT OPER, OPER_DESC, OPER_SHORT_DESC FROM WIPOPER WHERE"
                                      + "     FACTORY = ?"
                                      + " AND OPER IN ("
                                      + " SELECT DISTINCT OT.OPER FROM WIPOPER OT, WIPMATFLOWREL MF, WIPFLOWOPERREL FO WHERE"
                                      + "     OT.FACTORY = ?"
                                      + " AND OT.OPER >= ?"
                                      + " AND OT.OPER like ?"
                                      + " AND MF.FACTORY = OT.FACTORY"
                                      + " AND FO.FACTORY = OT.FACTORY"
                                      + " AND MF.MAT_ID = ?"
                                      + " AND FO.FLOW = MF.FLOW"
                                      + " AND OT.OPER = FO.OPER"
                                      + " ) ORDER BY OPER";

to Linq:

var strQuery = ctx.Wipoper.Where(t => t.Factory == sFactory && t.Oper.CompareTo(sOper) >= 0 && t.Oper.Contains(sOper)).Join(
                        ctx.Wipmatflowrel.Where(t => t.MatId == sMatId),
                        a => new { f = a.Factory },
                        b => new { f = b.Factory },
                        (a, b) => new
                        {
                            a.Factory,
                            a.Oper,
                            b.Flow,
                        }).Join(
                        ctx.Wipflowoperrel,
                        a => new { f = a.Factory, fl = a.Flow, op = a.Oper },
                        b => new { f = b.Factory, fl = b.Flow, op = b.Oper },
                        (a, b) => new
                        {
                            a.Oper,
                        }).ToList();
                    var strQuery1 = ctx.Wipoper.Where(t => t.Factory == sFactory && strQuery.Any(tt => tt.Oper == t.Oper)).ToList();

 

 

 

注:list轉為陣列:

string[] s = new string[strQuery.Count];
for (int i = 0; i < strQuery.Count; i++)
{
   s[i] = strQuery[i].Oper;
}