如果参数为-1,则需要针对是否指定了ID运行不同的查询...我该怎么做?我试过初始化var q;在If区外,但没有运气!
// 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中的输出:
{DisplayName = Tom}
不仅仅是'汤姆',我想要它!
答案 0 :(得分:2)
尝试
/ Loads by Entry ID, or if -1, by latest entry
private void LoadEntryByID(int EntryID)
{
dynamic q = null;
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 }).SingleOrDefault()
}).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()
}).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.DisplayName;
}
}
}
答案 1 :(得分:2)
tblBlogEntry myBlog;
if ( EntryID != -1 )
{
myBlog = db.tblBlogEntries
.SingleOrDefault( blog => blog.ID == EntryID );
}
else
{
myBlog = db.tblBlogEntries
.OrderByDescending( blog => blog.date ).FirstOrDefault();
}
this.Loaded = myBlog != null;
if ( this.Loaded )
{
this.ThisEntry = new EntryModel
{
ID = myBlog.ID,
Title = myBlog.title,
Entry = myBlog.entry,
Date = ( DateTime )myBlog.date,
UserID = ( int )myBlog.userID,
Username = db.yaf_Users
.Single( user => user.UserID == myBlog.userID ).DisplayName,
Comments = db.tblBlogComments
.Where( comment => comment.blogID == myBlog.ID ).Count()
}
}
答案 2 :(得分:1)
您的代码与t的方式有两个问题。首先,因为您只选择一个对象,所以不应将q声明为IEnumerable。如果选择范围,则可以这样做。
其次,既然你要获得一个tblBlogEntry对象,你应该如此声明Q,但是你不应该选择带有NEW的对象,因为这将使它成为一个匿名对象,你将获得{{ 1}}你需要选择它作为tblBlogEntry。但是,如果您不希望将整个对象与查询一起使用,或者需要在tblBlogEntry上添加更多您没有的字段,则应该创建一个仅具有所需属性的新类,然后您可以创建一个新的该类的实例:
将整个对象带到 ALL 其字段:
"CS0029: Cannot implicitly convert type 'AnonymousType#1' to 'tblBlogEntry'" error
带上一些特定的字段,或带有对象不具有的字段:
// Loads by Entry ID, or if -1, by latest entry
private void LoadEntryByID(int EntryID)
{
tblBlogEntry q;
if (EntryID == -1)
{
q = (from Blog in db.tblBlogEntries
orderby Blog.date descending
select Blog).SingleOrDefault();
}
else
{
q = (from Blog in db.tblBlogEntries
where Blog.ID == EntryID
select Blog).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;
}
}
}