问题:
它们是两个表TableEmails和TableSentEmailRecords。因此,无论何时发送电子邮件,TableSentEmailRecords中都会存储一条记录,例如发送时间等。
我想做一个LINQ查询,它使我得到TableEmails的列,再加上来自TableSentEmailRecords的“ SentDate”列,该列显示发送电子邮件的最新日期。
我尝试了这个:
List<CustomEmail> emails = new List<CustomEmail>();
var query = from c in db.TableEmails
join o in db.TableSentEmailRecords on c.EmailId equals o.EmailId
select new CustomEmail
{
EmailName = c.EmailName,
EmailId= c.EmailId,
EmailDescription = c.EmailDescription,
LastDateEmailSentOut = o.SentDate
};
emails = query.ToList();
但这不一定从TableSentEmailRecords表中获取该特定电子邮件的最新记录。
有任何想法吗 ???
最佳答案
您可以只使用嵌套查询而不是联接:
var query = from c in db.TableEmails
select new CustomEmail
{
EmailName = c.EmailName,
EmailId= c.EmailId,
EmailDescription= c.EmailDescription,
LastDateEmailSentOut = db.TableSentEmailRecordson
.Where(x => x.EmailId = c.EmailId)
.Max(x => x.SentDate)
};