我有我的階級結構如下加快LINQ查詢
public class Email
{
public string Subject {get;set;}
public string Message {get;set;}
public Contact Sender {get;set;}
public string SenderEmail {get;set;}
}
public class Contact
{
public string Email {get;set;}
public string Name {get;set;}
}
,我跑我的Linq查詢兩個部分 首先我選擇的所有電子郵件。
var query = from msg in context.Email
select msg;
然後我給你的聯繫方式,以電子郵件類
List<Email> outputList = new List<Email>();
foreach (var item in query.ToList())
{
var q = from contact in context.Contact
where contact.Email = item.SenderEmail
select contact;
item.Sender = q.SingleOrDefault();
outputList.Add(item);
}
return outputList;
反正是有,我可以不必運行多個查詢
爲什麼很多人寫了一些完全冗餘的東西,比如`from msg in context.Email選擇msg`而不是`context.Email`,然後抱怨它很慢? – Timwi 2011-01-18 22:54:57