猿问

在 Linq MongodbDriver 的“Where”方法中使用“Any”方法的问题

我刚开始使用 linq,但遇到了一些问题。我有一个大的 A 型集合和一个小的 B 型集合。我想要 A 中的项目列表,它们的“id”确实存在于 B 中。所以这是我认为可行的方法:

List<string> list = collection_A
                .Where(c => collection_B.Any(x => x.MessageId == c.Id))
                .Select(c=>c.Id)
                .ToList();

我在 .Net 中使用 mongoDB linq 提供程序,错误是:System.ArgumentException:不支持的过滤器。关系是 1-1

其实我不知道在这种情况下我是否应该使用“加入”或其他东西。


有只小跳蛙
浏览 76回答 2
2回答

慕盖茨4494581

我建议你试试这个:var&nbsp;messageIds&nbsp;=&nbsp;new&nbsp;HashSet<string>(collection_B.Select(x&nbsp;=>&nbsp;x.MessageId).Distinct()); List<string>&nbsp;list&nbsp;= &nbsp;&nbsp;&nbsp;&nbsp;collection_A &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Where(c&nbsp;=>&nbsp;messageIds.Contains(c.Id)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Select(c&nbsp;=>&nbsp;c.Id) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.ToList();

鸿蒙传说

如果我正确理解您的问题,以下代码将为您指明正确的方向。我使用 MongoDAL 进行数据访问,这只是 c# 驱动程序的抽象。using System;using System.Linq;using MongoDAL;namespace Example{&nbsp; &nbsp; class Person : Entity&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string Name { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; class BanRecord : Entity&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public One<Person> Person { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string ReasonForBan { get; set; }&nbsp; &nbsp; }&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DB("testdatabase");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var person1 = new Person { Name = "Person One" };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var person2 = new Person { Name = "Person Two" };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var person3 = new Person { Name = "Person Three" };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; person1.Save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; person2.Save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; person3.Save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var ban1 = new BanRecord&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Person = person1.ToReference(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReasonForBan = "Cause we can!"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ban1.Save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var ban2 = new BanRecord&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Person = person2.ToReference(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReasonForBan = "Cause we can!"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ban2.Save();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var bannedPeople = (from b in DB.Collection<BanRecord>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; join p in DB.Collection<Person>() on b.Person.ID equals p.ID into banned&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from p in banned&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; select p).ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.ReadKey();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答