我在使用Email.SendMail()时遇到问题。我需要将更多详细信息(如Event_Location,Event_Start等)添加到数据库中Events表中的“Email Msg”,但var项已经被事件调用。我想知道是否有办法在foreach循环或其他方法中放置两个var即事件和成员?
public void Send()
{
var events = from a in db.Members.ToList()
join e in db.Events.ToList() on a.Hobby_Code equals e.Hobby_Code
where a.Postal_Code == e.Postal_Code || e.Event_Status ==("A")
var member= from w in db.Members.TOList()
join z in db.Events.ToList() on w.Event_ID equals z.Event_ID
select w
foreach (var item in events)
Email.SendMail(item.Member_Email_Address,"Subject","Email Msg",false);
}
会员的模型类
public class Member
{
[Key]
[ScaffoldColumn(false)]
public int Member_ID { get; set; }
[Required(ErrorMessage = "A Section Name is required")]
[DisplayName("Section Name")]
public string Postal_Code { get; set; }
[Required(ErrorMessage = "An email address is required")]
[DisplayName("Email Address")]
public string Member_Email_Address { get; set; }
[DisplayName("Hobby Code")]
[Required(ErrorMessage = "A Hobby is required")]
public string Hobby_Code { get; set; }
[DisplayName("Event ID")]
public int? Event_ID { get; set; }
public string FullName
{
get
{
return Member_Last_Name + " " + Member_First_Name;
}
}
public virtual Section Section { get; set; }
public virtual Event Event { get; set; }
}
活动的模型类
public class Event
{
[ScaffoldColumn(false)]
[Key]
public int Event_ID { get; set; }
[Required(ErrorMessage = "A Event Location is required")]
[DisplayName("Event Location")]
public string Postal_Code { get; set; }
[DisplayName("Event Status")]
public string Event_Status { get; set; }
[DisplayName("Event Start Date")]
[Required]
public DateTime Event_Start { get; set; }
[DisplayName("Event End Date")]
[Required]
public DateTime Event_End { get; set; }
[DisplayName("Event Type")]
[Required(ErrorMessage = "A Event Type is required")]
public string Hobby_Code { get; set; }
[Required]
[StringLength(100)]
[DataType(DataType.MultilineText)]
[DisplayName(" Event Description")]
public string Event_Description { get; set; }
答案 0 :(得分:1)
您可以使用匿名方法仅返回您想要的属性,即:
var events = from a in db.Members.ToList()
join e in db.Events.ToList() on a.Hobby_Code equals e.Hobby_Code
where a.Postal_Code == e.Postal_Code || e.Event_Status ==("A")
select new { email = a.Member_Email_Address, location = e.Event_Location, eventStart = e.Event_Start};
然后在for循环中访问它,如下所示:
foreach (var item in events)
Email.SendMail(item.email,"Subject",String.Format("Location: {0}",item.location),false); // including all the other variables required and excluding the formatting
答案 1 :(得分:0)
如果您正确使用ASP .NET MVC的自动实体映射,它将为您执行所有连接。在会员中,您应该引用Event event
而不是Event_ID
。然后你可以简单地做:
foreach (var member in db.Members)
{
String msgBody = "You are invited to " && member.event.Event_Description && " on " && member.event.Event_Start;
Email.SendMail(member.Member_Email_Address,"Subject", msgBody, false);
}