【发布时间】:2017-09-02 00:30:54
【问题描述】:
我一生都无法弄清楚这个 LINQ 查询需要是什么。我在标准 LINQ 查询上做得很好,但在这种情况下,我需要检查集合中列表中项目的条件。
任务:
我需要删除集合中的所有消息,其中他们的收件人列表没有包含特定字符串的所有句柄,即“thiscompany.com”。
我目前的尝试:
// Remove internal messages
var internalMessages = messages._results
.Where(x => x.recipients.All(y => y.handle.Contains("somecompany.com")));
messages._results = messages._results.Except(internalMessages).ToList();
据我了解,这种方法可以正常工作,我只需要对 Message 对象实施相等检查,但由于某些原因,我无法做到这一点。我知道 LINQ 必须有一种方法来执行一个单行程序,它根据收件人列表的这个子条件删除所有消息,但我就是不知道从这里去哪里。
消息对象:
public class Message
{
public _Links _links { get; set; }
public string id { get; set; }
public string type { get; set; }
public bool is_inbound { get; set; }
public float created_at { get; set; }
public string blurb { get; set; }
public Author author { get; set; }
public List<Recipient> recipients { get; set; }
public string body { get; set; }
public string text { get; set; }
public List<Attachment> attachments { get; set; }
public MessageMetadata metadata { get; set; }
}
收件人对象:
public class Recipient
{
public _Links _links { get; set; }
public string handle { get; set; }
public string role { get; set; }
}
【问题讨论】: