1

我正在编写一个应用程序,允许我们的用户向他们的联系人发送新闻通讯。这是数据库结构(仅重要字段):

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 对象,这样我就可以遍历它并为每个联系人(而不是每个电子邮件地址)发送一封电子邮件。

我有 Newsletter 对象可以开始使用。所以在伪它将是......

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()
                        }));

任何帮助是极大的赞赏!

4

1 回答 1

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()
        }));

尝试第一个选择 asSelectMany而不是Select

于 2013-09-24T03:06:07.057 回答