实体框架排除结果

时间:2015-08-06 09:06:28

标签: c# asp.net-mvc linq entity-framework entity

我想要排除查询结果。

E.g我有:

Table_1 (Id, name)
Table_2 (Id, Table_1_Id)

而且,我想写一个查询,它只返回Table_1Table_2中不存在的var query = db.Table_1.Include("Table_2").Where(???? 的结果。这是一对多的关系。

有什么想法吗?

C:\Users\id122302\Documents\test.js:11
        path.exists(full_path,function(exists)
             ^
TypeError: undefined is not a function
    at Server.<anonymous> (C:\Users\id122302\Documents\test.js:11:7)
    at Server.emit (events.js:110:17)
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:491:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:111:23)
    at Socket.socketOnData (_http_server.js:343:22)
    at Socket.emit (events.js:107:17)
    at readableAddChunk (_stream_readable.js:163:16)
    at Socket.Readable.push (_stream_readable.js:126:10)
    at TCP.onread (net.js:538:20)

1 个答案:

答案 0 :(得分:1)

您应该使用Except

var query = db.Table_1.Select(MapToType).Except(db.Table_2.Select(MapToSameType));

如果您使用的旧版EF不支持,可以使用All

var query = db.Table_1.Where(x => db.Table_2.All(y => y.Table_1_Id != x.Id);