linq'不在'查询不能解决我的期望

时间:2012-06-29 09:11:43

标签: linq

我在Linq编写了以下查询:

var res =  dc.TransactionLoggings
             .Where(
                x => !dc.TrsMessages(y => y.DocId != x.DocId)
              ).Select(x => x.CCHMessage).ToList();

这解决了以下问题:

SELECT [t0].[CCHMessage]
FROM [dbo].[TransactionLogging] AS [t0]
WHERE NOT (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [dbo].[TrsMessages] AS [t1]
    WHERE [t1].[DocId] <> [t0].[DocId]
    ))

始终返回null

Basiaclly我想写的是以下内容:

Select cchmessage
from transactionlogging
where docid not in (select docid from trsmessages)

有关LINQ语句错误的任何建议吗?

1 个答案:

答案 0 :(得分:1)

var res =  dc.TransactionLoggings
             .Where(tl => !dc.TrsMessages.Any(trsm=> trsm.DocId == tl.DocId))
             .Select(x => x.CCHMessage).ToList();

   var trsMessagesDocId = dc.TrsMessages.Select(trsm => trsm.DocId).ToList();

   var res = dc.TransactionLoggins
                .Where(tl => !trsMessagesDocId.Contains(tl.DocId))
                .Select(tl => tl.CCHMEssage)
                .ToList();
相关问题