如果参数为-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