如果參數爲-1,則需要運行不同的查詢以確定是否指定了ID ......我該怎麼做?我試過初始化var q;在If塊之外,但沒有運氣!Linq作用域問題+減少重複代碼
// Loads by Entry ID, or if -1, by latest entry
private void LoadEntryByID(int EntryID)
{
IEnumerable<tblBlogEntry> q;
if (EntryID == -1)
{
q = (
from Blog in db.tblBlogEntries
orderby Blog.date descending
select new
{
Blog.ID,
Blog.title,
Blog.entry,
Blog.date,
Blog.userID,
Comments = (
from BlogComments in db.tblBlogComments
where BlogComments.blogID == Blog.ID
select BlogComments).Count(),
Username = (
from Users in db.yaf_Users
where Users.UserID == Blog.userID
select new { Users.DisplayName })
}).FirstOrDefault();
}
else
{
q = (
from Blog in db.tblBlogEntries
where Blog.ID == EntryID
select new
{
Blog.ID,
Blog.title,
Blog.entry,
Blog.date,
Blog.userID,
Comments = (
from BlogComments in db.tblBlogComments
where BlogComments.blogID == Blog.ID
select BlogComments).Count(),
Username = (
from Users in db.yaf_Users
where Users.UserID == Blog.userID
select new { Users.DisplayName })
}).SingleOrDefault();
}
if (q == null)
{
this.Loaded = false;
}
else
{
this.ID = q.ID;
this.Title = q.title;
this.Entry = q.entry;
this.Date = (DateTime)q.date;
this.UserID = (int)q.userID;
this.Loaded = true;
this.AuthorUsername = q.Username;
}
}
我的主要目的是減少重複的代碼
編輯
按照一些答案我現在已經有了:
// Blog data model
public class EntryModel
{
public int ID { get; set; }
public string Title { get; set; }
public string Entry { get; set; }
public DateTime Date { get; set; }
public int UserID { get; set; }
public string Username { get; set; }
public int Comments { get; set; }
}
// A single blog entry
public class BlogEntry
{
public bool Loaded; // Did this entry load OK?
private DataClassesDataContext db = new DataClassesDataContext();
public EntryModel ThisEntry = new EntryModel();
// Initialisers
public BlogEntry(int EntryID)
{
this.LoadEntryByID(EntryID);
}
public BlogEntry()
{
this.LoadLatest();
}
public void LoadLatest()
{
this.LoadEntryByID(-1);
}
// Loads by Entry ID, or if -1, by latest entry
private void LoadEntryByID(int EntryID)
{
EntryModel q = null;
if (EntryID == -1)
{
q = (from Blog in db.tblBlogEntries
orderby Blog.date descending
select new EntryModel
{
Title = Blog.title,
ID = Blog.ID,
Entry = Blog.entry,
Date = (DateTime)Blog.date,
UserID = (int)Blog.userID,
Comments = (from BlogComments in db.tblBlogComments where BlogComments.blogID == Blog.ID select BlogComments).Count(),
Username = (from Users in db.yaf_Users where Users.UserID == Blog.userID select new { Users.DisplayName }).SingleOrDefault().ToString()
}
).FirstOrDefault();
}
else
{
q = (from Blog in db.tblBlogEntries
where Blog.ID == EntryID
select new EntryModel
{
Title = Blog.title,
ID = Blog.ID,
Entry = Blog.entry,
Date = (DateTime)Blog.date,
UserID = (int)Blog.userID,
Comments = (from BlogComments in db.tblBlogComments where BlogComments.blogID == Blog.ID select BlogComments).Count(),
Username = (from Users in db.yaf_Users where Users.UserID == Blog.userID select new { Users.DisplayName }).SingleOrDefault().ToString()
}).SingleOrDefault();
}
if (q == null)
{
this.Loaded = false;
}
else
{
this.ThisEntry.ID = q.ID;
this.ThisEntry.Title = q.Title;
this.ThisEntry.Entry = q.Entry;
this.ThisEntry.Date = q.Date;
this.ThisEntry.UserID = q.UserID;
this.Loaded = true;
this.ThisEntry.Username = q.Username;
this.ThisEntry.Comments = q.Comments;
}
}
}
這工作,但拋出了很多VS中的藍色波浪線,以及
this.ThisEntry.Username = q.Username;
輸出在HTML中:
{顯示名稱=湯姆}
不只是 '湯姆',因爲我希望它!
在IEnumerable之外重新定義塊之外的塊? –
CarneyCode
2011-03-06 15:37:05
作爲一般說明,我將這兩個作爲截然不同的呼叫,而是有一個if/else somehwere。這樣,商業意義後來就很清楚了,而不是隱含的「-1意味着最新的條目」 – Larsbj 2011-03-06 15:47:27
這段代碼不會編譯。您試圖將一個匿名類型的IEnumerable賦值給IEnumerable類型的變量'q'。此外(如果編譯代碼),代碼'this.Loaded = false;'永遠不會被調用,因爲'q'總是有一個值。 –
Steven
2011-03-06 16:03:40