Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq Scope Problem + Reduce Repeated Code
    primarykey
    data
    text
    <p>If the parameter is -1, it needs to run a different query as to if an ID was specified... how do I do this? I've tried initialising var q; outside the If block but no luck!</p> <pre><code> // Loads by Entry ID, or if -1, by latest entry private void LoadEntryByID(int EntryID) { IEnumerable&lt;tblBlogEntry&gt; 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; } } </code></pre> <p>My main aim is to reduce repeating code</p> <p><strong>Edit</strong></p> <p>As per some answers I've now got:</p> <pre><code>// 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; } } } </code></pre> <p>This works, but throws a lot of blue squiggly lines in VS, and </p> <pre><code>this.ThisEntry.Username = q.Username; </code></pre> <p>Outputs in HTML to:</p> <blockquote> <p>{ DisplayName = Tom }</p> </blockquote> <p>Not just 'Tom' as I want it to!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload