Skip to content

Commit ecd6f2d

Browse files
authored
Merge pull request BlogEngine#108 from ThisWillDoIt/master
Improved some LINQ expressions
2 parents 9bccf0e + 166a4ca commit ecd6f2d

30 files changed

+66
-68
lines changed

BlogEngine/BlogEngine.Core/Data/BlogRepository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Models.Blog FindById(Guid id)
5252
if (!(Blog.CurrentInstance.IsPrimary && Security.IsAdministrator))
5353
throw new UnauthorizedAccessException();
5454

55-
var blog = Blog.Blogs.Where(b => b.Id == id).FirstOrDefault();
55+
var blog = Blog.Blogs.FirstOrDefault(b => b.Id == id);
5656
return ToJson(blog);
5757
}
5858

@@ -92,7 +92,7 @@ public bool Update(Models.Blog blog)
9292
throw new UnauthorizedAccessException();
9393
try
9494
{
95-
var coreBlog = Blog.Blogs.Where(b => b.Id == blog.Id).FirstOrDefault();
95+
var coreBlog = Blog.Blogs.FirstOrDefault(b => b.Id == blog.Id);
9696
return Save(coreBlog, blog);
9797
}
9898
catch (Exception)
@@ -113,7 +113,7 @@ public bool Remove(Guid id)
113113
throw new UnauthorizedAccessException();
114114
try
115115
{
116-
var blog = Blog.Blogs.Where(b => b.Id == id).FirstOrDefault();
116+
var blog = Blog.Blogs.FirstOrDefault(b => b.Id == id);
117117
blog.Delete();
118118
blog.Save();
119119
return true;

BlogEngine/BlogEngine.Core/Data/CategoryRepository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public IEnumerable<CategoryItem> Find(int take = 10, int skip = 0, string filter
4242
// add categories without posts
4343
foreach (var c in Category.Categories)
4444
{
45-
var x = items.Where(i => i.Id == c.Id).FirstOrDefault();
45+
var x = items.FirstOrDefault(i => i.Id == c.Id);
4646
if (x == null)
4747
items.Add(new CategoryItem { Id = c.Id, Parent = OptionById(c.Parent), Title = c.Title, Description = c.Description, Count = 0 });
4848
}
@@ -87,11 +87,11 @@ public CategoryItem FindById(Guid id)
8787
// add categories without posts
8888
foreach (var c in Category.Categories)
8989
{
90-
var x = items.Where(i => i.Id == c.Id).FirstOrDefault();
90+
var x = items.FirstOrDefault(i => i.Id == c.Id);
9191
if (x == null)
9292
items.Add(new CategoryItem { Id = c.Id, Parent = OptionById(c.Parent), Title = c.Title, Description = c.Description, Count = 0 });
9393
}
94-
return items.Where(c => c.Id == id).FirstOrDefault();
94+
return items.FirstOrDefault(c => c.Id == id);
9595
}
9696
/// <summary>
9797
/// Add new item

BlogEngine/BlogEngine.Core/Data/CommentsRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public CommentItem Add(CommentDetail item)
8282
var c = new Comment();
8383
try
8484
{
85-
var post = Post.Posts.Where(p => p.Id == item.PostId).FirstOrDefault();
85+
var post = Post.Posts.FirstOrDefault(p => p.Id == item.PostId);
8686

8787
c.Id = Guid.NewGuid();
8888
c.ParentId = item.ParentId;
@@ -104,7 +104,7 @@ public CommentItem Add(CommentDetail item)
104104
post.AddComment(c);
105105
post.Save();
106106

107-
var newComm = post.Comments.Where(cm => cm.Content == c.Content).FirstOrDefault();
107+
var newComm = post.Comments.FirstOrDefault(cm => cm.Content == c.Content);
108108
return Json.GetComment(newComm, post.Comments);
109109
}
110110
catch (Exception ex)

BlogEngine/BlogEngine.Core/Data/CustomFieldRepository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public CustomField FindById(string type, string id, string key)
4949
if (!Security.IsAuthorizedTo(Rights.AccessAdminPages))
5050
throw new UnauthorizedAccessException();
5151

52-
var cf = Find("").Where(f => f.CustomType == type && f.ObjectId == id && f.Key == key).FirstOrDefault();
52+
var cf = Find("").FirstOrDefault(f => f.CustomType == type && f.ObjectId == id && f.Key == key);
5353
return cf;
5454
}
5555

@@ -157,10 +157,10 @@ public void ClearCustomFields(string type, string id)
157157

158158
bool AlreadyExists(CustomField item)
159159
{
160-
var field = CustomFieldsParser.CachedFields.Where(f => f.BlogId == item.BlogId
160+
var field = CustomFieldsParser.CachedFields.FirstOrDefault(f => f.BlogId == item.BlogId
161161
&& f.CustomType == item.CustomType
162162
&& f.ObjectId == item.ObjectId
163-
&& f.Key == item.Key).FirstOrDefault();
163+
&& f.Key == item.Key);
164164
return field != null;
165165
}
166166

BlogEngine/BlogEngine.Core/Data/PageRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ static string GetUniqueSlug(string slug)
189189

190190
private static bool IsUniqueSlug(string slug)
191191
{
192-
return Page.Pages.Where(p => p.Slug != null && p.Slug.ToLower() == slug.ToLower())
193-
.FirstOrDefault() == null ? true : false;
192+
return Page.Pages
193+
.FirstOrDefault(p => p.Slug != null && p.Slug.ToLower() == slug.ToLower()) == null ? true : false;
194194
}
195195

196196
// if description not set, use first 100 chars in the post

BlogEngine/BlogEngine.Core/Data/PostRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ static void UpdatePostCategories(Post post, List<CategoryItem> categories)
172172
foreach (var cat in categories)
173173
{
174174
// add if category does not exist
175-
var existingCat = Category.Categories.Where(c => c.Title == cat.Title).FirstOrDefault();
175+
var existingCat = Category.Categories.FirstOrDefault(c => c.Title == cat.Title);
176176
if (existingCat == null)
177177
{
178178
var repo = new CategoryRepository();
@@ -208,7 +208,7 @@ static List<TagItem> FilterTags(List<TagItem> tags)
208208

209209
foreach (var t in tags)
210210
{
211-
if (!uniqueTags.Any(u => u.TagName == t.TagName))
211+
if (uniqueTags.All(u => u.TagName != t.TagName))
212212
{
213213
uniqueTags.Add(t);
214214
}
@@ -233,8 +233,8 @@ static string GetUniqueSlug(string slug)
233233

234234
static bool IsUniqueSlug(string slug)
235235
{
236-
return Post.ApplicablePosts.Where(p => p.Slug != null && p.Slug.ToLower() == slug.ToLower())
237-
.FirstOrDefault() == null ? true : false;
236+
return Post.ApplicablePosts
237+
.FirstOrDefault(p => p.Slug != null && p.Slug.ToLower() == slug.ToLower()) == null ? true : false;
238238
}
239239

240240
// if description not set, use first 100 chars in the post

BlogEngine/BlogEngine.Core/Data/RolesRepository.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public RoleItem FindById(string id)
5353
var roles = new List<Data.Models.RoleItem>();
5454
roles.AddRange(System.Web.Security.Roles.GetAllRoles().Select(r => new Data.Models.RoleItem { RoleName = r, IsSystemRole = Security.IsSystemRole(r) }));
5555

56-
return roles.Where(r => r.RoleName.ToLower() == id.ToLower()).FirstOrDefault();
56+
return roles.FirstOrDefault(r => r.RoleName.ToLower() == id.ToLower());
5757
}
5858

5959
/// <summary>
@@ -101,7 +101,7 @@ public bool Update(RoleItem role, string oldRole)
101101
if (!Security.IsAuthorizedTo(Rights.EditRoles))
102102
throw new System.UnauthorizedAccessException();
103103

104-
var updateRole = System.Web.Security.Roles.GetAllRoles().Where(r => r.ToString() == oldRole).FirstOrDefault();
104+
var updateRole = Roles.GetAllRoles().FirstOrDefault(r => r.ToString() == oldRole);
105105

106106
if (updateRole == null)
107107
throw new ApplicationException("Role not found");
@@ -172,7 +172,7 @@ public IEnumerable<Group> GetRoleRights(string role)
172172

173173
var category = rightDetails == null ? RightCategory.General : rightDetails.Category;
174174

175-
var group = groups.Where(g => g.Title == category.ToString()).FirstOrDefault();
175+
var group = groups.FirstOrDefault(g => g.Title == category.ToString());
176176

177177
var prm = new Permission();
178178
var rt = Right.GetRightByName(right.ToString());

BlogEngine/BlogEngine.Core/Data/Services/CustomFieldsParser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,17 @@ static string ReplaceCustomFields(string html)
117117

118118
if (s[1] == "POST")
119119
{
120-
var cf = postFields.Where(f => f.Key.ToLower() == key.ToLower()
121-
&& f.ObjectId.ToLower() == id.ToLower()).FirstOrDefault();
120+
var cf = postFields.FirstOrDefault(f => f.Key.ToLower() == key.ToLower()
121+
&& f.ObjectId.ToLower() == id.ToLower());
122122

123123
if (cf != null)
124124
val = cf.Value;
125125
}
126126

127127
if (s[1] == "THEME")
128128
{
129-
var cf = themeFields.Where(f => f.Key.ToLower() == key.ToLower()
130-
&& f.ObjectId.ToLower() == id.ToLower()).FirstOrDefault();
129+
var cf = themeFields.FirstOrDefault(f => f.Key.ToLower() == key.ToLower()
130+
&& f.ObjectId.ToLower() == id.ToLower());
131131

132132
if (cf != null)
133133
val = cf.Value;

BlogEngine/BlogEngine.Core/Data/Services/Json.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public static CommentItem GetComment(Comment c, List<Comment> postComments)
141141
jc.Title = c.Teaser.Length < 80 ? c.Teaser : c.Teaser.Substring(0, 80) + "...";
142142
jc.DateCreated = c.DateCreated.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
143143
jc.RelativeLink = c.RelativeLink;
144-
jc.HasChildren = postComments.Where(pc => pc.ParentId == c.Id).FirstOrDefault() != null;
144+
jc.HasChildren = postComments.FirstOrDefault(pc => pc.ParentId == c.Id) != null;
145145
jc.Avatar = Gravatar(c);
146146
return jc;
147147
}
@@ -223,7 +223,7 @@ static SelectOption ItemParent(Guid? id)
223223
if (id == null || id == Guid.Empty)
224224
return null;
225225

226-
var item = Category.Categories.Where(c => c.Id == id).FirstOrDefault();
226+
var item = Category.Categories.FirstOrDefault(c => c.Id == id);
227227
return new SelectOption { OptionName = item.Title, OptionValue = item.Id.ToString() };
228228
}
229229

BlogEngine/BlogEngine.Core/Data/StatsRepository.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public Stats Get()
2626
if (!Security.IsAuthorizedTo(Rights.EditOtherUsersPosts))
2727
postList = postList.Where(p => p.Author.ToLower() == Security.CurrentUser.Identity.Name.ToLower()).ToList();
2828

29-
stats.PublishedPostsCount = postList.Where(p => p.IsPublished == true).Count();
30-
stats.DraftPostsCount = postList.Where(p => p.IsPublished == false).Count();
29+
stats.PublishedPostsCount = postList.Count(p => p.IsPublished == true);
30+
stats.DraftPostsCount = postList.Count(p => p.IsPublished == false);
3131

32-
stats.PublishedPagesCount = Page.Pages.Where(p => p.IsPublished == true && p.IsDeleted == false).Count();
33-
stats.DraftPagesCount = Page.Pages.Where(p => p.IsPublished == false && p.IsDeleted == false).Count();
32+
stats.PublishedPagesCount = Page.Pages.Count(p => p.IsPublished == true && p.IsDeleted == false);
33+
stats.DraftPagesCount = Page.Pages.Count(p => p.IsPublished == false && p.IsDeleted == false);
3434

3535
CountComments(stats);
3636

@@ -70,9 +70,9 @@ void CountComments(Stats stats)
7070
if (post.Author.ToLower() != Security.CurrentUser.Identity.Name.ToLower())
7171
continue;
7272

73-
stats.PublishedCommentsCount += post.Comments.Where(c => c.IsPublished == true && c.IsDeleted == false).Count();
74-
stats.UnapprovedCommentsCount += post.Comments.Where(c => c.IsPublished == false && c.IsSpam == false && c.IsDeleted == false).Count();
75-
stats.SpamCommentsCount += post.Comments.Where(c => c.IsPublished == false && c.IsSpam == true && c.IsDeleted == false).Count();
73+
stats.PublishedCommentsCount += post.Comments.Count(c => c.IsPublished == true && c.IsDeleted == false);
74+
stats.UnapprovedCommentsCount += post.Comments.Count(c => c.IsPublished == false && c.IsSpam == false && c.IsDeleted == false);
75+
stats.SpamCommentsCount += post.Comments.Count(c => c.IsPublished == false && c.IsSpam == true && c.IsDeleted == false);
7676
}
7777
}
7878

BlogEngine/BlogEngine.Core/Data/TrashRepository.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ public bool Restore(string trashType, Guid id)
152152
}
153153
break;
154154
case "Post":
155-
var delPost = Post.DeletedPosts.Where(p => p.Id == id).FirstOrDefault();
155+
var delPost = Post.DeletedPosts.FirstOrDefault(p => p.Id == id);
156156
if (delPost != null) delPost.Restore();
157157
break;
158158
case "Page":
159-
var delPage = Page.DeletedPages.Where(pg => pg.Id == id).FirstOrDefault();
159+
var delPage = Page.DeletedPages.FirstOrDefault(pg => pg.Id == id);
160160
if (delPage != null) delPage.Restore();
161161
break;
162162
default:
@@ -189,11 +189,11 @@ public bool Purge(string trashType, Guid id)
189189
}
190190
break;
191191
case "Post":
192-
var delPost = Post.DeletedPosts.Where(p => p.Id == id).FirstOrDefault();
192+
var delPost = Post.DeletedPosts.FirstOrDefault(p => p.Id == id);
193193
if (delPost != null) delPost.Purge();
194194
break;
195195
case "Page":
196-
var delPage = Page.DeletedPages.Where(pg => pg.Id == id).FirstOrDefault();
196+
var delPage = Page.DeletedPages.FirstOrDefault(pg => pg.Id == id);
197197
if (delPage != null) delPage.Purge();
198198
break;
199199
default:

BlogEngine/BlogEngine.Core/Data/ViewModels/DashboardVM.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ private void LoadPosts()
123123

124124
foreach (var p in posts)
125125
{
126-
ApprovedCommentsCnt += p.Comments.Where(c => c.IsPublished && !c.IsDeleted).Count();
127-
PendingCommentsCnt += p.Comments.Where(c => !c.IsPublished && !c.IsSpam && !c.IsDeleted).Count();
128-
SpamCommentsCnt += p.Comments.Where(c => !c.IsPublished && c.IsSpam && !c.IsDeleted).Count();
126+
ApprovedCommentsCnt += p.Comments.Count(c => c.IsPublished && !c.IsDeleted);
127+
PendingCommentsCnt += p.Comments.Count(c => !c.IsPublished && !c.IsSpam && !c.IsDeleted);
128+
SpamCommentsCnt += p.Comments.Count(c => !c.IsPublished && c.IsSpam && !c.IsDeleted);
129129
_comments.AddRange(p.Comments);
130130
}
131131
}
@@ -146,7 +146,7 @@ private void LoadTrash()
146146
{
147147
var posts = Post.DeletedPosts;
148148
_trash = new List<TrashItem>();
149-
if (posts.Count() > 0)
149+
if (posts.Any())
150150
{
151151
foreach (var p in posts)
152152
{
@@ -162,7 +162,7 @@ private void LoadTrash()
162162
}
163163
}
164164
var pages = Page.DeletedPages;
165-
if (pages.Count() > 0)
165+
if (pages.Any())
166166
{
167167
foreach (var page in pages)
168168
{
@@ -187,7 +187,7 @@ private void LoadTrash()
187187

188188
comms.AddRange(p.DeletedComments);
189189
}
190-
if (comms.Count() > 0)
190+
if (comms.Any())
191191
{
192192
foreach (var c in comms)
193193
{

BlogEngine/BlogEngine.Core/Helpers/BlogGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public static bool ValidateProperties(string blogName, string userName, string e
116116
return false;
117117
}
118118

119-
if (Blog.Blogs.Where(b => b.Name.ToLower() == blogName.ToLower()).FirstOrDefault() != null)
119+
if (Blog.Blogs.FirstOrDefault(b => b.Name.ToLower() == blogName.ToLower()) != null)
120120
{
121121
message = "Blog with this name already exists; Please select different name.";
122122
return false;

BlogEngine/BlogEngine.Core/Providers/DbProvider/DbBlogProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ public override bool SetupNewBlog(Blog newBlog, string userName, string email, s
707707
{
708708
if (conn.HasConnection)
709709
{
710-
Blog existingBlog = Blog.Blogs.Where(b => b.Name == "Template").FirstOrDefault();
710+
Blog existingBlog = Blog.Blogs.FirstOrDefault(b => b.Name == "Template");
711711

712712
if (existingBlog == null)
713713
existingBlog = Blog.Blogs[0];

BlogEngine/BlogEngine.Core/Providers/FileSystemProviders/DbFileSystemProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public override void DeleteDirectory(string VirtualPath)
189189
public override bool DirectoryExists(string VirtualPath)
190190
{
191191
VirtualPath = VirtualPath.VirtualPathToDbPath();
192-
return new FileSystem.FileStoreDb(this.connectionString).FileStoreDirectories.Where(x => x.FullPath.ToLower() == VirtualPath.ToLower() && x.BlogID == Blog.CurrentInstance.Id).Count() > 0;
192+
return new FileSystem.FileStoreDb(this.connectionString).FileStoreDirectories.Any(x => x.FullPath.ToLower() == VirtualPath.ToLower() && x.BlogID == Blog.CurrentInstance.Id);
193193
}
194194

195195
/// <summary>

BlogEngine/BlogEngine.Core/Providers/XmlProvider/Settings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public override void SaveSettings(StringDictionary settings)
5757
throw new ArgumentNullException("settings");
5858
}
5959

60-
var filename = string.Format("{0}settings.xml", Folder);
60+
var filename = $"{Folder}settings.xml";
6161
var writerSettings = new XmlWriterSettings { Indent = true };
6262

6363
// ------------------------------------------------------------

BlogEngine/BlogEngine.Core/Services/Packaging/FileSystem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ static string PackageType(List<PackageFile> files)
449449
/// <returns>Version number</returns>
450450
public static string GetInstalledVersion(string pkgId)
451451
{
452-
var pkg = BlogService.InstalledFromGalleryPackages().Where(p => p.PackageId == pkgId).FirstOrDefault();
452+
var pkg = BlogService.InstalledFromGalleryPackages().FirstOrDefault(p => p.PackageId == pkgId);
453453
return pkg == null ? "" : pkg.Version;
454454
}
455455

BlogEngine/BlogEngine.Core/Services/Packaging/Gallery.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public static void Load(List<Package> packages)
4848
if (string.IsNullOrEmpty(jp.IconUrl))
4949
jp.IconUrl = DefaultThumbnail("");
5050

51-
if (extras != null && extras.Count() > 0)
51+
if (extras != null && extras.Any())
5252
{
53-
var extra = extras.Where(e => e.Id.ToLower() == pkg.Id.ToLower() + "." + pkg.Version).FirstOrDefault();
53+
var extra = extras.FirstOrDefault(e => e.Id.ToLower() == pkg.Id.ToLower() + "." + pkg.Version);
5454

5555
if (extra != null)
5656
{

BlogEngine/BlogEngine.Core/Services/Security/Security.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ public static bool IsAuthorizedTo(Rights right)
371371
/// <returns></returns>
372372
public static bool IsAuthorizedTo(AuthorizationCheck authCheck, IEnumerable<Rights> rights)
373373
{
374-
if (rights.Count() == 0)
374+
if (!rights.Any())
375375
{
376376
// Always return false for this. If there's a mistake where authorization
377377
// is being checked for on an empty collection, we don't want to return

BlogEngine/BlogEngine.Core/Services/Syndication/BlogML/BlogReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private void LoadBlogExtendedPosts(BlogMLBlog blog)
344344
if (post.PostType == BlogPostTypes.Normal)
345345
{
346346
BlogMLPost p = post;
347-
blogsExtended.Where(b => b.PostUrl == p.PostUrl).FirstOrDefault().BlogPost = post;
347+
blogsExtended.FirstOrDefault(b => b.PostUrl == p.PostUrl).BlogPost = post;
348348
}
349349
}
350350
}

BlogEngine/BlogEngine.Core/Web/Extensions/ExtensionManager.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,17 +168,15 @@ public static ExtensionSettings GetSettings(string extensionName, string setting
168168

169169
if (!Blog.CurrentInstance.IsPrimary && extension.SubBlogEnabled)
170170
{
171-
return extension.Settings.Where(
172-
setting => setting != null
171+
return extension.Settings.FirstOrDefault(setting => setting != null
173172
&& setting.Name == settingName
174-
&& setting.BlogId == Blog.CurrentInstance.Id).FirstOrDefault();
173+
&& setting.BlogId == Blog.CurrentInstance.Id);
175174
}
176175

177176
var primId = Blog.Blogs.FirstOrDefault(b => b.IsPrimary).BlogId;
178-
return extension.Settings.Where(
179-
setting => setting != null
177+
return extension.Settings.FirstOrDefault(setting => setting != null
180178
&& setting.Name == settingName
181-
&& (setting.BlogId == primId || setting.BlogId == null)).FirstOrDefault();
179+
&& (setting.BlogId == primId || setting.BlogId == null));
182180
}
183181

184182
/// <summary>

0 commit comments

Comments
 (0)