4

我正在尝试排除a.id存在于db.AdminAdjusterStatus.

from u in db.Users
join a in db.Adjusters on u.id equals a.userID
where u.userType.ToLower() == "adjuster"
   && !(from x in db.AdminAdjusterStatus select x.adjusterID).Contains(a.id)
select new AdjusterProfileStatusItem { user = u, adjuster = a }

上面的代码说,“其中 NOT 包含a.iddb.AdminAdjusterStatus.adjusterID.

问题是,它不起作用。我有这两个条目db.AdminAdjusterStatus

A9EC05B5-651D-4AA7-8275-1F6BFE212C03
1BDE55D9-ED0A-4854-9D5F-B89DB17F02D2

而且,LINQ 查询给了我:

A9EC05B5-651D-4AA7-8275-1F6BFE212C03
1BDE55D9-ED0A-4854-9D5F-B89DB17F02D2
e21ff49c-9505-495d-b4a3-c259ee3459d6

然而,它应该只给我:

e21ff49c-9505-495d-b4a3-c259ee3459d6
4

4 回答 4

1
var query = from u in db.Users
             from a in db.Adjusters                         
             where u.userType.ToLower() == "adjuster" && 
             u.id == a.userID && 
             !db.AdminAdjusterStatus.Any(i => i.adjusterID == a.id)

...

于 2013-07-18T12:56:32.643 回答
1

也许这可以工作?

            var AdjusterItems = from aa in AdminAdjusterStatus
                            join a in Adjusters
                            on aa.AdjusterId equals a.AdjusterId
                            select a.UserId;

        var UsersNotAdjustAdmin = from u in Users
                                  where !AdjusterItems.Any(x => x == u.Id)
                                  && u.UserType.Equals("Adjuster", StringComparison.InvariantCultureIgnoreCase)
                                  select u;

        var result = from u in UsersNotAdjustAdmin
                     join a in Adjusters
                     on u.Id equals a.UserId
                     select new AdjusterProfileStatusItem() { Adjuster = a, User = u };
于 2013-07-18T13:39:27.437 回答
1

感谢大家的帮助,很抱歉浪费了任何人的时间。我意识到这一行:

obj.adjusterID = '@(Url.RequestContext.RouteData.Values["id"])';

我的代码中的其他地方一直在发送u.id,而不是a.id像我想象的那样发送。因此,我在数据库中保存了错误的 ID,从而导致了我遇到的问题。

再次感谢你的帮助!

于 2013-07-18T13:36:55.993 回答
1

您可以尝试对空记录进行左连接和过滤。如果没有要测试的数据,我无法确定它是否会“开箱即用”,但我已经多次使用以下代码进行这样的过滤:

db.Users.Join(db.Abjusters, outer => outer.id, inner => inner.userID, new { User = outer, Adjuster = inner })
    .GroupJoin(DBConcurrencyException.AdminAdjusterStatus, outer => outer.Adjuster.id, inner => inner.adjusterID, new { User = outer.User, Adjuster = outer.Adjuster, Admins = inner })
    .SelectMany(grp => grp.Admins.DefaultIfEmpty(), (grp, admin) => new { User = grp.User, Adjuster = grp.Adjuster, Admin = admin })
    .Where(item => item.User.userType == "adjuster" && item.Admin == null)
    .Select(item => new AdjusterProfileStatusItem { user = item.User, adjuster = item.Adjuster });

GroupJoin/SelectMany 组合执行左连接,然后您可以过滤对象为 null 的位置,这应该产生与 NOT IN 相同的结果。

于 2013-07-18T13:28:45.647 回答