Skip to content

Commit

Permalink
simplify some linq (OrchardCMS#15249)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Feb 4, 2024
1 parent 0555ff2 commit 01057a5
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 20 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -2816,6 +2816,15 @@
"contributions": [
"eventOrganizing"
]
},
{
"login": "SimonCropp",
"name": "Simon Cropp",
"avatar_url": "https://avatars.githubusercontent.com/u/122666?v=4",
"profile": "https://github.com/SimonCropp",
"contributions": [
"code"
]
}
],
"skipCi": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public async Task BuildNavigationAsync(MenuItem menuItem, NavigationBuilder buil
{
try
{
var treeBuilder = treeNodeBuilders.Where(x => x.Name == childNode.GetType().Name).FirstOrDefault();
var treeBuilder = treeNodeBuilders.FirstOrDefault(x => x.Name == childNode.GetType().Name);
await treeBuilder.BuildNavigationAsync(childNode, builder, treeNodeBuilders);
}
catch (Exception e)
Expand Down Expand Up @@ -119,8 +119,7 @@ private static List<string> GetIconClasses(ContentTypeDefinition contentType, Co
else
{
var typeEntry = node.ContentTypes
.Where(x => string.Equals(x.ContentTypeId, contentType.Name, StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();
.FirstOrDefault(x => string.Equals(x.ContentTypeId, contentType.Name, StringComparison.OrdinalIgnoreCase));

return AddPrefixToClasses(typeEntry.IconClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public IActionResult Create()
public async Task<IActionResult> Edit(string todoId)
{
var model = (await _session.Query<TodoModel>().ListAsync())
.Where(m => m.TodoId == todoId)
.FirstOrDefault();
.FirstOrDefault(m => m.TodoId == todoId);

if (model == null)
{
Expand All @@ -73,8 +72,7 @@ public async Task<IActionResult> Update(TodoViewModel viewModel, string returnUr
if (ModelState.IsValid)
{
var model = (await _session.Query<TodoModel>().ListAsync())
.Where(m => m.TodoId == viewModel.TodoId)
.FirstOrDefault();
.FirstOrDefault(m => m.TodoId == viewModel.TodoId);

model ??= new TodoModel() { TodoId = viewModel.TodoId };

Expand All @@ -99,8 +97,7 @@ public async Task<IActionResult> Update(TodoViewModel viewModel, string returnUr
public async Task<IActionResult> Delete(string todoId)
{
var model = (await _session.Query<TodoModel>().ListAsync())
.Where(m => m.TodoId == todoId)
.FirstOrDefault();
.FirstOrDefault(m => m.TodoId == todoId);

if (model == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public async Task<ActionResult> Index(string format)
var bestFormatterMatch = _feedFormatProviders
.Select(provider => provider.Match(context))
.Where(match => match != null && match.FeedBuilder != null)
.OrderByDescending(match => match.Priority)
.FirstOrDefault();
.MaxBy(match => match.Priority);

if (bestFormatterMatch == null || bestFormatterMatch.FeedBuilder == null)
{
Expand All @@ -56,8 +55,7 @@ public async Task<ActionResult> Index(string format)

var bestQueryMatch = queryMatches
.Where(match => match != null && match.FeedQuery != null)
.OrderByDescending(match => match.Priority)
.FirstOrDefault();
.MaxBy(match => match.Priority);

if (bestQueryMatch == null || bestQueryMatch.FeedQuery == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ await builder.AddAsync(new LocalizedString(_contentType.DisplayName, _contentTyp
{
try
{
var treeBuilder = treeNodeBuilders.Where(x => x.Name == childNode.GetType().Name).FirstOrDefault();
var treeBuilder = treeNodeBuilders.FirstOrDefault(x => x.Name == childNode.GetType().Name);
await treeBuilder.BuildNavigationAsync(childNode, builder, treeNodeBuilders);
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// ['fr-FR', 'en-US']
var supportedCultures = JConvert.SerializeObject(Model.Cultures.Where(x => x.Supported).Select(c => c.CultureInfo.Name).ToArray());
var allCultures = JConvert.SerializeObject(Model.Cultures.Select(c => new { Name = c.CultureInfo.Name, DisplayName = !string.IsNullOrEmpty(c.CultureInfo.DisplayName) ? c.CultureInfo.DisplayName : c.CultureInfo.NativeName, Supported = c.Supported }).ToArray());
var defaultCulture = Model.Cultures.Where(x => x.IsDefault).FirstOrDefault().CultureInfo.Name;
var defaultCulture = Model.Cultures.FirstOrDefault(x => x.IsDefault).CultureInfo.Name;
var selectedCulture = Model.Cultures.Except(Model.Cultures.Where(x => x.Supported)).First().CultureInfo.Name;
}

Expand Down
4 changes: 2 additions & 2 deletions src/OrchardCore/OrchardCore.Abstractions/Modules/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public Module(string assemblyName, bool isApplication = false)
var moduleInfos = Assembly.GetCustomAttributes<ModuleAttribute>();

ModuleInfo =
moduleInfos.Where(f => f is not ModuleMarkerAttribute).FirstOrDefault()
?? moduleInfos.Where(f => f is ModuleMarkerAttribute).FirstOrDefault()
moduleInfos.FirstOrDefault(f => f is not ModuleMarkerAttribute)
?? moduleInfos.FirstOrDefault(f => f is ModuleMarkerAttribute)
// This is better use the default parameterless ctor and assign the property.
?? new ModuleAttribute() { Name = assemblyName };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void EnsureMatchings()
public LiquidTagHelperActivator GetActivator(string helper, IEnumerable<string> arguments)
{
EnsureMatchings();
var matching = _matchings.Where(d => d.Match(helper, arguments)).FirstOrDefault() ?? LiquidTagHelperMatching.None;
var matching = _matchings.FirstOrDefault(d => d.Match(helper, arguments)) ?? LiquidTagHelperMatching.None;

if (matching != LiquidTagHelperMatching.None)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public KeyValuePairs[] Properties
get
{
return _shape.Properties
.Cast<KeyValuePair<string, object>>()
.Select(entry => new KeyValuePairs(entry.Key, entry.Value))
.ToArray();
}
Expand Down
3 changes: 2 additions & 1 deletion src/docs/community/contributors/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Contributors ✨

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-303-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-304-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key))! You can add new contributors by [using the All Contributors bot](https://allcontributors.org/docs/en/bot/usage).
Expand Down Expand Up @@ -415,6 +415,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center" valign="top" width="16.66%"><a href="https://github.com/vjacquet"><img src="https://avatars.githubusercontent.com/u/6363920?v=4?s=100" width="100px;" alt="vjacquet"/><br /><sub><b>vjacquet</b></sub></a><br /><a href="https://github.com/OrchardCMS/OrchardCore/commits?author=vjacquet" title="Code">💻</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://github.com/yaricrolletservico"><img src="https://avatars.githubusercontent.com/u/101557629?v=4?s=100" width="100px;" alt="yaricrolletservico"/><br /><sub><b>yaricrolletservico</b></sub></a><br /><a href="https://github.com/OrchardCMS/OrchardCore/commits?author=yaricrolletservico" title="Code">💻</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://github.com/viktoriamagyar"><img src="https://avatars.githubusercontent.com/u/84029837?v=4?s=100" width="100px;" alt="Viktória Magyar"/><br /><sub><b>Viktória Magyar</b></sub></a><br /><a href="#eventOrganizing-viktoriamagyar" title="Event Organizing">📋</a></td>
<td align="center" valign="top" width="16.66%"><a href="https://github.com/SimonCropp"><img src="https://avatars.githubusercontent.com/u/122666?v=4?s=100" width="100px;" alt="Simon Cropp"/><br /><sub><b>Simon Cropp</b></sub></a><br /><a href="https://github.com/OrchardCMS/OrchardCore/commits?author=SimonCropp" title="Code">💻</a></td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit 01057a5

Please sign in to comment.