1

在按条件从数据库中获取行时,我的 linq2sql 出现问题,select n rows from table1 where table2 doesn't contain any rows by condition table1.id != table2.accId and table1.action != "action_constant"我的 c# 中有这样的代码:

query = (from accsArray in db.Accs
         where accsArray.Valid == 1 &&
               !String.IsNullOrEmpty(accsArray.Password) && 
               accsArray.vip_rec == 0 || accsArray.data_col == null &&
               !db.Reaktors.Any(rk => rk.Action == action &&
                                      rk.Account_id == accsArray.id)

         select new DbAcc
         {
             id = accsArray.id,
             Login = accsArray.Login,
             Password = accsArray.Password,
             System = accsArray.System,
             //FriendsCount = Convert.ToInt32(accsArray.Friends_count)
         }).Take(count).OrderBy(acc => acc.id);

!db.Reaktors.Any(rk => rk.Action == action && rk.Account_id == accsArray.id)这不起作用,在寻找我干净的 sql 代码时,我看到了这个:

SELECT [t2].[id], [t2].[Login], [t2].[System], [t2].[Password]
FROM (SELECT TOP (50) [t0].[id], [t0].[Login], [t0].[System], [t0].[Password]
      FROM [Collider].[dbo].[Accs] AS [t0]
      WHERE (([t0].[Valid] = 1)
             AND ([t0].[Password] <> '')
             AND ([t0].[Login] <> '')
             AND ([t0].[data_col] = 0))
         OR (([t0].[data_col] IS NULL)
             AND (NOT (EXISTS(SELECT NULL AS [EMPTY]
                              FROM [dbo].[Reaktor] AS [t1]
                              WHERE [t1].[Actionself] = '_take_acc_dc'
                                AND ([t1].[Account_id] = [t0].[id])))))) AS [t2]
ORDER BY [t2].[id]

SELECT NULL AS [EMPTY] FROM [dbo].[Reaktor]特别害怕,NULL AS [EMPTY]所以我认为这就是它无法正常工作的原因。

4

1 回答 1

2

解决方案是正确放置括号并订购条件。良好的工作代码将如下所示:

query = (from accsArray in db.Accs
         where !db.Reaktors.Any(rk => (rk.Actionself == action 
                                       && rk.Account_id == accsArray.id)) 
               && accsArray.Valid == 1 
               && accsArray.Password != String.Empty 
               && accsArray.Login != String.Empty 
               && accsArray.data_col == 0
         select new DbAcc
         {
             id = accsArray.id,
             Login = accsArray.Login,
             Password = accsArray.Password,
             System = accsArray.System,
         }).Take(count).OrderBy(acc => acc.id);
于 2013-03-05T00:50:59.923 回答