我正在编写一个应用程序,允许我们的用户向他们的联系人发送简报。这是数据库结构(仅限重要字段):
Contacts(tbl) ContactEmails(tbl) Newsletters(tbl)
ContactID ContactEmailID NewsletterID
... ContactID (fk) ...
EmailAddress
Deleted
...
NewsletterLists(tbl) NewsletterToLists(tbl)
NewsletterListID NewsletterID (fk)
... NewsletterListID (fk)
NewsletterListToContacts(tbl)
NewsletterListID (fk)
ContactID (fk)
Active
Deleted
UnsubscribedOn
...
现在我的课程结构为:
public class NewsletterContactEmails
{
public int ContactID { get; set; }
public List<string> EmailList { get; set; }
}
我希望每个联系人都有一个NewsletterContactEmails对象,以便我可以遍历它并为每个联系人发送一封电子邮件(而不是每个电子邮件地址)。
我有新闻稿对象开始使用。所以在伪中它会......
1. Get NewsletterLists this newsletter is to send to.
2. Get Active (double opted-in) and non-deleted/unsubscribed DISTINCT Contact List from said NewsletterLists.
3. Create NewsletterContactEmails object for each contact and add their non-deleted DISTINCT list of email addresses.
我想在一个查询中执行此操作。这是我现在拥有的,我认为我很接近,但这甚至不会编译:
List<NewsletterContactEmails> Recipients = objNewsletter.NewsletterToLists
.Select(ntl => ntl.NewsletterList.NewsletterListToContacts
.Where(nl => nl.Active == true && nl.Deleted == false && nl.UnsubscribedOn == null)
.Select(nl => new NewsletterContactEmails {
ContactID = nl.ContactID,
EmailList = nl.Contact.ContactEmails.Where(ce => ce.Deleted == false).Select(ce => ce.EmailAddress).Distinct().ToList()
}));
非常感谢任何帮助!
答案 0 :(得分:3)
List<NewsletterContactEmails> Recipients = objNewsletter.NewsletterToLists
.SelectMany(ntl => ntl.NewsletterList.NewsletterListToContacts
.Where(nl => nl.Active == true && nl.Deleted == false && nl.UnsubscribedOn == null)
.Select(nl => new NewsletterContactEmails
{
ContactID = nl.ContactID,
EmailList =
nl.Contact.ContactEmails.Where(ce => ce.Deleted == false)
.Select(ce => ce.EmailAddress)
.Distinct()
.ToList()
}));
首先选择SelectMany
而不是Select