Added QueryHelper

This commit is contained in:
FH 2022-01-01 17:22:01 +01:00
parent 2613cb68d8
commit ad1465e339
10 changed files with 149 additions and 69 deletions

View file

@ -9,23 +9,7 @@
// <copyright file="ExtensionsEx.cs" company="FCS"> // <copyright file="ExtensionsEx.cs" company="FCS">
// Copyright © FCS 2015-2022 // Copyright © FCS 2015-2022
// </copyright> // </copyright>
// <summary> // <summary></summary>
// Part of FCS.Lib - a set of utilities for C# - pieced together from fragments
// Copyright (C) 2021 FCS
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// </summary>
// *********************************************************************** // ***********************************************************************
using System; using System;

View file

@ -56,6 +56,7 @@
<Compile Include="IAsyncReadonlyRepo.cs" /> <Compile Include="IAsyncReadonlyRepo.cs" />
<Compile Include="IRepositoryEx.cs" /> <Compile Include="IRepositoryEx.cs" />
<Compile Include="Mogrifiers.cs" /> <Compile Include="Mogrifiers.cs" />
<Compile Include="QueryHelper.cs" />
<Compile Include="StringOptions.cs" /> <Compile Include="StringOptions.cs" />
<Compile Include="Properties\AssemblyInfo.cs"> <Compile Include="Properties\AssemblyInfo.cs">
<DependentUpon>AssemblyInfo.tt</DependentUpon> <DependentUpon>AssemblyInfo.tt</DependentUpon>
@ -66,6 +67,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="FCS.Lib.pfx" /> <None Include="FCS.Lib.pfx" />
<None Include="LICENSE.md" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include=".gitignore" /> <Content Include=".gitignore" />

View file

@ -1,2 +0,0 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp90</s:String></wpf:ResourceDictionary>

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup> <PropertyGroup>
<ProjectView>ProjectFiles</ProjectView> <ProjectView>ShowAllFiles</ProjectView>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View file

@ -10,6 +10,7 @@
// Copyright © FCS 2015-2022 // Copyright © FCS 2015-2022
// </copyright> // </copyright>
// <summary> // <summary>
// derived from https://sourceforge.net/projects/shorturl-dotnet
// Part of FCS.Lib - a set of utilities for C# - pieced together from fragments // Part of FCS.Lib - a set of utilities for C# - pieced together from fragments
// Copyright (C) 2021 FCS // Copyright (C) 2021 FCS
// //
@ -55,7 +56,7 @@ namespace FCS.Lib
/// </summary> /// </summary>
/// <param name="length">The lengt h.</param> /// <param name="length">The lengt h.</param>
/// <returns>System.String.</returns> /// <returns>System.String.</returns>
/// <remarks>derived from https://sourceforge.net/projects/shorturl-dotnet/</remarks> /// <remarks>derived from https://sourceforge.net/projects/shorturl-dotnet </remarks>
public static string ShortUrlGenerator(int length) public static string ShortUrlGenerator(int length)
{ {
const string charsLower = "cdfghjkmnpqrstvwxyz"; const string charsLower = "cdfghjkmnpqrstvwxyz";
@ -139,10 +140,7 @@ namespace FCS.Lib
// this group. // this group.
if (lastCharIdx != nextCharIdx) if (lastCharIdx != nextCharIdx)
{ {
var temp = charGroups[nextGroupIdx][lastCharIdx]; (charGroups[nextGroupIdx][lastCharIdx], charGroups[nextGroupIdx][nextCharIdx]) = (charGroups[nextGroupIdx][nextCharIdx], charGroups[nextGroupIdx][lastCharIdx]);
charGroups[nextGroupIdx][lastCharIdx] =
charGroups[nextGroupIdx][nextCharIdx];
charGroups[nextGroupIdx][nextCharIdx] = temp;
} }
// Decrement the number of unprocessed characters in // Decrement the number of unprocessed characters in
@ -162,10 +160,7 @@ namespace FCS.Lib
// so that we don't pick it until we process all groups. // so that we don't pick it until we process all groups.
if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx) if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
{ {
var temp = leftGroupsOrder[lastLeftGroupsOrderIdx]; (leftGroupsOrder[lastLeftGroupsOrderIdx], leftGroupsOrder[nextLeftGroupsOrderIdx]) = (leftGroupsOrder[nextLeftGroupsOrderIdx], leftGroupsOrder[lastLeftGroupsOrderIdx]);
leftGroupsOrder[lastLeftGroupsOrderIdx] =
leftGroupsOrder[nextLeftGroupsOrderIdx];
leftGroupsOrder[nextLeftGroupsOrderIdx] = temp;
} }
// Decrement the number of unprocessed groups. // Decrement the number of unprocessed groups.

View file

@ -4,7 +4,7 @@
// Created : 03-10-2015 // Created : 03-10-2015
// //
// Last Modified By : FH // Last Modified By : FH
// Last Modified On : 2021-02-24 // Last Modified On : 2021-11-13
// *********************************************************************** // ***********************************************************************
// <copyright file="IRepositoryEx.cs" company="FCS"> // <copyright file="IRepositoryEx.cs" company="FCS">
// Copyright © FCS 2015-2022 // Copyright © FCS 2015-2022
@ -41,13 +41,6 @@ namespace FCS.Lib
/// <typeparam name="TEntity">The type of the t entity</typeparam> /// <typeparam name="TEntity">The type of the t entity</typeparam>
public interface IRepositoryEx<TEntity> where TEntity : class public interface IRepositoryEx<TEntity> where TEntity : class
{ {
/// <summary>
/// Get all entities asynchronous
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>Task&lt;System.Boolean&gt;</returns>
Task<bool> AllAsync(Expression<Func<TEntity, bool>> predicate);
/// <summary> /// <summary>
/// Get all entities synchronous /// Get all entities synchronous
/// </summary> /// </summary>
@ -102,17 +95,11 @@ namespace FCS.Lib
bool Any(Expression<Func<TEntity, bool>> predicate); bool Any(Expression<Func<TEntity, bool>> predicate);
/// <summary> /// <summary>
/// Get all entities /// Get entity by id
/// </summary> /// </summary>
/// <returns>IQueryable&lt;TEntity&gt;</returns> /// <param name="id">The identifier.</param>
IQueryable<TEntity> All(); /// <returns>TEntity</returns>
TEntity GetById(string id);
/// <summary>
/// Find all matching entities matching query
/// </summary>
/// <param name="predicate">Predicate</param>
/// <returns>IQueryable&lt;TEntity&gt;</returns>
IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
/// <summary> /// <summary>
/// Find first entity matching query /// Find first entity matching query
@ -129,10 +116,16 @@ namespace FCS.Lib
TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate); TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
/// <summary> /// <summary>
/// Get entity by id /// Find all matching entities matching query
/// </summary> /// </summary>
/// <param name="id">The identifier.</param> /// <param name="predicate">Predicate</param>
/// <returns>TEntity</returns> /// <returns>IQueryable&lt;TEntity&gt;</returns>
TEntity GetById(string id); IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
/// <summary>
/// Get all entities
/// </summary>
/// <returns>IQueryable&lt;TEntity&gt;</returns>
IQueryable<TEntity> All();
} }
} }

View file

@ -119,7 +119,7 @@ namespace FCS.Lib
/// <returns>T.</returns> /// <returns>T.</returns>
public static T IntToEnum<T>(int value) public static T IntToEnum<T>(int value)
{ {
return (T) Enum.ToObject(typeof(T), value); return (T)Enum.ToObject(typeof(T), value);
} }
/// <summary> /// <summary>
@ -171,7 +171,7 @@ namespace FCS.Lib
{ {
var empty = string.Empty; var empty = string.Empty;
if (list == null) return empty; if (list == null) return empty;
var enumerator = (IEnumerator) list.GetType().GetMethod("GetEnumerator")?.Invoke(list, null); var enumerator = (IEnumerator)list.GetType().GetMethod("GetEnumerator")?.Invoke(list, null);
while (enumerator != null && enumerator.MoveNext()) while (enumerator != null && enumerator.MoveNext())
if (enumerator.Current != null) if (enumerator.Current != null)
empty = string.Concat(empty, enumerator.Current.ToString(), delimiter); empty = string.Concat(empty, enumerator.Current.ToString(), delimiter);
@ -228,8 +228,8 @@ namespace FCS.Lib
return return
!decimal.TryParse(inString.Replace(",", "").Replace(".", ""), NumberStyles.Number, !decimal.TryParse(inString.Replace(",", "").Replace(".", ""), NumberStyles.Number,
CultureInfo.InvariantCulture, out var num) CultureInfo.InvariantCulture, out var num)
? (decimal?) null ? null
: decimal.Divide(num, new decimal((long) 100)); : decimal.Divide(num, new decimal((long)100));
} }
/// <summary> /// <summary>
@ -240,7 +240,7 @@ namespace FCS.Lib
/// <returns>T.</returns> /// <returns>T.</returns>
public static T StringToEnum<T>(string value) public static T StringToEnum<T>(string value)
{ {
return (T) Enum.Parse(typeof(T), value, true); return (T)Enum.Parse(typeof(T), value, true);
} }
/// <summary> /// <summary>
@ -263,6 +263,11 @@ namespace FCS.Lib
/// <returns>List&lt;T&gt;.</returns> /// <returns>List&lt;T&gt;.</returns>
/// <exception cref="ArgumentNullException">value</exception> /// <exception cref="ArgumentNullException">value</exception>
/// <exception cref="ArgumentNullException">delimiter</exception> /// <exception cref="ArgumentNullException">delimiter</exception>
/// <exception cref="ArgumentNullException">value</exception>
/// <exception cref="ArgumentNullException">delimiter</exception>
/// <exception cref="ArgumentNullException">value</exception>
/// <exception cref="ArgumentNullException">delimiter</exception>
/// <exception cref="ArgumentNullException">value</exception>
public static List<T> StringToList<T>(string value, string delimiter) public static List<T> StringToList<T>(string value, string delimiter)
{ {
if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value)); if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value));
@ -277,16 +282,16 @@ namespace FCS.Lib
var upperInvariant = o.ToUpperInvariant(); var upperInvariant = o.ToUpperInvariant();
if (string.CompareOrdinal(upperInvariant, "system.string") == 0) if (string.CompareOrdinal(upperInvariant, "system.string") == 0)
{ {
ts.Add((T) Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture)); ts.Add((T)Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture));
} }
else if (string.CompareOrdinal(upperInvariant, "system.int32") == 0) else if (string.CompareOrdinal(upperInvariant, "system.int32") == 0)
{ {
ts.Add((T) Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture)); ts.Add((T)Convert.ChangeType(str, typeof(T), CultureInfo.InvariantCulture));
} }
else if (string.CompareOrdinal(upperInvariant, "system.guid") == 0) else if (string.CompareOrdinal(upperInvariant, "system.guid") == 0)
{ {
var guid = new Guid(str); var guid = new Guid(str);
ts.Add((T) Convert.ChangeType(guid, typeof(T), CultureInfo.InvariantCulture)); ts.Add((T)Convert.ChangeType(guid, typeof(T), CultureInfo.InvariantCulture));
} }
} }
@ -333,12 +338,12 @@ namespace FCS.Lib
var dt = DateTime.UtcNow.ToString("yy MM dd HH MM ss"); var dt = DateTime.UtcNow.ToString("yy MM dd HH MM ss");
var sb = new StringBuilder(); var sb = new StringBuilder();
var dts = dt.Split(' '); var dts = dt.Split(' ');
sb.Append((char) int.Parse(dts[0]) + 65); sb.Append((char)int.Parse(dts[0]) + 65);
sb.Append((char) int.Parse(dts[1]) + 65); sb.Append((char)int.Parse(dts[1]) + 65);
sb.Append((char) int.Parse(dts[2]) + 97); sb.Append((char)int.Parse(dts[2]) + 97);
sb.Append((char) int.Parse(dts[3]) + 65); sb.Append((char)int.Parse(dts[3]) + 65);
sb.Append((char) int.Parse(dts[4]) + 97); sb.Append((char)int.Parse(dts[4]) + 97);
sb.Append((char) int.Parse(dts[5]) + 97); sb.Append((char)int.Parse(dts[5]) + 97);
return sb.ToString(); return sb.ToString();
} }

103
QueryHelper.cs Normal file
View file

@ -0,0 +1,103 @@
// ***********************************************************************
// Assembly : FCS.Lib
// Author : FH
// Created : 2020-07-01
//
// Last Modified By : FH
// Last Modified On : 2021-02-24
// ***********************************************************************
// <copyright file="QueryHelper.cs" company="Frede Hundewadt">
// Copyright © FCS 2015-2022
// </copyright>
// <summary>
// Implementation of https://stackoverflow.com/a/45761590
// Part of FCS.Lib - a set of utilities for C# - pieced together from fragments
// Copyright (C) 2021 FCS
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// </summary>
// ***********************************************************************
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace FCS.Lib
{
/// <summary>
/// Class QueryHelper.
/// </summary>
public static class QueryHelper
{
/// <summary>
/// Orders the by.
/// </summary>
/// <typeparam name="TModel">The type of the t model.</typeparam>
/// <param name="q">The q.</param>
/// <param name="name">The name.</param>
/// <param name="desc">if set to <c>true</c> [desc].</param>
/// <returns>IQueryable&lt;TModel&gt;.</returns>
public static IQueryable<TModel> OrderBy<TModel>(this IQueryable<TModel> q, string name, bool desc)
{
var entityType = typeof(TModel);
var p = entityType.GetProperty(name);
var m = typeof(QueryHelper).GetMethod("OrderByProperty")?.MakeGenericMethod(entityType, p.PropertyType);
return (IQueryable<TModel>)m.Invoke(null, new object[] { q, p, desc });
}
/// <summary>
/// Thens the by.
/// </summary>
/// <typeparam name="TModel">The type of the t model.</typeparam>
/// <param name="q">The q.</param>
/// <param name="name">The name.</param>
/// <param name="desc">if set to <c>true</c> [desc].</param>
/// <returns>IQueryable&lt;TModel&gt;.</returns>
public static IQueryable<TModel> ThenBy<TModel>(this IQueryable<TModel> q, string name, bool desc)
{
var entityType = typeof(TModel);
var p = entityType.GetProperty(name);
var m = typeof(QueryHelper).GetMethod("OrderByProperty")?.MakeGenericMethod(entityType, p.PropertyType);
return (IQueryable<TModel>)m.Invoke(null, new object[] { q, p, desc });
}
/// <summary>
/// Orders the by property.
/// </summary>
/// <typeparam name="TModel">The type of the t model.</typeparam>
/// <typeparam name="TRet">The type of the t ret.</typeparam>
/// <param name="q">The q.</param>
/// <param name="p">The p.</param>
/// <param name="desc">if set to <c>true</c> [desc].</param>
/// <returns>IQueryable&lt;TModel&gt;.</returns>
public static IQueryable<TModel> OrderByProperty<TModel, TRet>(IQueryable<TModel> q, PropertyInfo p, bool desc)
{
var pe = Expression.Parameter(typeof(TModel));
Expression se = Expression.Convert(Expression.Property(pe, p), typeof(object));
var exp = Expression.Lambda<Func<TModel, TRet>>(se, pe);
return desc ? q.OrderByDescending(exp) : q.OrderBy(exp);
}
}
}

View file

@ -47,7 +47,7 @@ namespace FCS.Lib
/// <summary> /// <summary>
/// A read-only object of the Squid struct. /// A read-only object of the Squid struct.
/// Value is guaranteed to be all zeroes. /// Value is guaranteed to be all zeroes.
/// Equivalent to <see cref="Guid.Empty" />. /// Equivalent to Guid.Empty" />.
/// </summary> /// </summary>
public static readonly Squid Empty = new(Guid.Empty); public static readonly Squid Empty = new(Guid.Empty);
@ -129,12 +129,12 @@ namespace FCS.Lib
} }
/// <summary> /// <summary>
/// Initialises a new object of the Squid using <see cref="Guid.NewGuid()" />. /// Initialises a new object of the Squid using Guid.NewGuid()" />.
/// </summary> /// </summary>
/// <returns>New Squid object</returns> /// <returns>New Squid object</returns>
public static Squid NewGuid() public static Squid NewGuid()
{ {
return new(Guid.NewGuid()); return new Squid(Guid.NewGuid());
} }
/// <summary> /// <summary>