Added QueryHelper
This commit is contained in:
parent
2613cb68d8
commit
ad1465e339
10 changed files with 149 additions and 69 deletions
|
@ -9,23 +9,7 @@
|
|||
// <copyright file="ExtensionsEx.cs" company="FCS">
|
||||
// Copyright © FCS 2015-2022
|
||||
// </copyright>
|
||||
// <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>
|
||||
// <summary></summary>
|
||||
// ***********************************************************************
|
||||
|
||||
using System;
|
||||
|
|
|
@ -56,6 +56,7 @@
|
|||
<Compile Include="IAsyncReadonlyRepo.cs" />
|
||||
<Compile Include="IRepositoryEx.cs" />
|
||||
<Compile Include="Mogrifiers.cs" />
|
||||
<Compile Include="QueryHelper.cs" />
|
||||
<Compile Include="StringOptions.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs">
|
||||
<DependentUpon>AssemblyInfo.tt</DependentUpon>
|
||||
|
@ -66,6 +67,7 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="FCS.Lib.pfx" />
|
||||
<None Include="LICENSE.md" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include=".gitignore" />
|
||||
|
|
|
@ -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>
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ProjectView>ProjectFiles</ProjectView>
|
||||
<ProjectView>ShowAllFiles</ProjectView>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -10,6 +10,7 @@
|
|||
// Copyright © FCS 2015-2022
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// derived from https://sourceforge.net/projects/shorturl-dotnet
|
||||
// Part of FCS.Lib - a set of utilities for C# - pieced together from fragments
|
||||
// Copyright (C) 2021 FCS
|
||||
//
|
||||
|
@ -55,7 +56,7 @@ namespace FCS.Lib
|
|||
/// </summary>
|
||||
/// <param name="length">The lengt h.</param>
|
||||
/// <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)
|
||||
{
|
||||
const string charsLower = "cdfghjkmnpqrstvwxyz";
|
||||
|
@ -139,10 +140,7 @@ namespace FCS.Lib
|
|||
// this group.
|
||||
if (lastCharIdx != nextCharIdx)
|
||||
{
|
||||
var temp = charGroups[nextGroupIdx][lastCharIdx];
|
||||
charGroups[nextGroupIdx][lastCharIdx] =
|
||||
charGroups[nextGroupIdx][nextCharIdx];
|
||||
charGroups[nextGroupIdx][nextCharIdx] = temp;
|
||||
(charGroups[nextGroupIdx][lastCharIdx], charGroups[nextGroupIdx][nextCharIdx]) = (charGroups[nextGroupIdx][nextCharIdx], charGroups[nextGroupIdx][lastCharIdx]);
|
||||
}
|
||||
|
||||
// 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.
|
||||
if (lastLeftGroupsOrderIdx != nextLeftGroupsOrderIdx)
|
||||
{
|
||||
var temp = leftGroupsOrder[lastLeftGroupsOrderIdx];
|
||||
leftGroupsOrder[lastLeftGroupsOrderIdx] =
|
||||
leftGroupsOrder[nextLeftGroupsOrderIdx];
|
||||
leftGroupsOrder[nextLeftGroupsOrderIdx] = temp;
|
||||
(leftGroupsOrder[lastLeftGroupsOrderIdx], leftGroupsOrder[nextLeftGroupsOrderIdx]) = (leftGroupsOrder[nextLeftGroupsOrderIdx], leftGroupsOrder[lastLeftGroupsOrderIdx]);
|
||||
}
|
||||
|
||||
// Decrement the number of unprocessed groups.
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
// Created : 03-10-2015
|
||||
//
|
||||
// Last Modified By : FH
|
||||
// Last Modified On : 2021-02-24
|
||||
// Last Modified On : 2021-11-13
|
||||
// ***********************************************************************
|
||||
// <copyright file="IRepositoryEx.cs" company="FCS">
|
||||
// Copyright © FCS 2015-2022
|
||||
|
@ -41,13 +41,6 @@ namespace FCS.Lib
|
|||
/// <typeparam name="TEntity">The type of the t entity</typeparam>
|
||||
public interface IRepositoryEx<TEntity> where TEntity : class
|
||||
{
|
||||
/// <summary>
|
||||
/// Get all entities asynchronous
|
||||
/// </summary>
|
||||
/// <param name="predicate">Predicate</param>
|
||||
/// <returns>Task<System.Boolean></returns>
|
||||
Task<bool> AllAsync(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// Get all entities synchronous
|
||||
/// </summary>
|
||||
|
@ -102,17 +95,11 @@ namespace FCS.Lib
|
|||
bool Any(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// Get all entities
|
||||
/// Get entity by id
|
||||
/// </summary>
|
||||
/// <returns>IQueryable<TEntity></returns>
|
||||
IQueryable<TEntity> All();
|
||||
|
||||
/// <summary>
|
||||
/// Find all matching entities matching query
|
||||
/// </summary>
|
||||
/// <param name="predicate">Predicate</param>
|
||||
/// <returns>IQueryable<TEntity></returns>
|
||||
IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
|
||||
/// <param name="id">The identifier.</param>
|
||||
/// <returns>TEntity</returns>
|
||||
TEntity GetById(string id);
|
||||
|
||||
/// <summary>
|
||||
/// Find first entity matching query
|
||||
|
@ -129,10 +116,16 @@ namespace FCS.Lib
|
|||
TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// Get entity by id
|
||||
/// Find all matching entities matching query
|
||||
/// </summary>
|
||||
/// <param name="id">The identifier.</param>
|
||||
/// <returns>TEntity</returns>
|
||||
TEntity GetById(string id);
|
||||
/// <param name="predicate">Predicate</param>
|
||||
/// <returns>IQueryable<TEntity></returns>
|
||||
IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
|
||||
|
||||
/// <summary>
|
||||
/// Get all entities
|
||||
/// </summary>
|
||||
/// <returns>IQueryable<TEntity></returns>
|
||||
IQueryable<TEntity> All();
|
||||
}
|
||||
}
|
|
@ -119,7 +119,7 @@ namespace FCS.Lib
|
|||
/// <returns>T.</returns>
|
||||
public static T IntToEnum<T>(int value)
|
||||
{
|
||||
return (T) Enum.ToObject(typeof(T), value);
|
||||
return (T)Enum.ToObject(typeof(T), value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -171,7 +171,7 @@ namespace FCS.Lib
|
|||
{
|
||||
var empty = string.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())
|
||||
if (enumerator.Current != null)
|
||||
empty = string.Concat(empty, enumerator.Current.ToString(), delimiter);
|
||||
|
@ -228,8 +228,8 @@ namespace FCS.Lib
|
|||
return
|
||||
!decimal.TryParse(inString.Replace(",", "").Replace(".", ""), NumberStyles.Number,
|
||||
CultureInfo.InvariantCulture, out var num)
|
||||
? (decimal?) null
|
||||
: decimal.Divide(num, new decimal((long) 100));
|
||||
? null
|
||||
: decimal.Divide(num, new decimal((long)100));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -240,7 +240,7 @@ namespace FCS.Lib
|
|||
/// <returns>T.</returns>
|
||||
public static T StringToEnum<T>(string value)
|
||||
{
|
||||
return (T) Enum.Parse(typeof(T), value, true);
|
||||
return (T)Enum.Parse(typeof(T), value, true);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -263,6 +263,11 @@ namespace FCS.Lib
|
|||
/// <returns>List<T>.</returns>
|
||||
/// <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>
|
||||
/// <exception cref="ArgumentNullException">delimiter</exception>
|
||||
/// <exception cref="ArgumentNullException">value</exception>
|
||||
public static List<T> StringToList<T>(string value, string delimiter)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value)) throw new ArgumentNullException(nameof(value));
|
||||
|
@ -277,16 +282,16 @@ namespace FCS.Lib
|
|||
var upperInvariant = o.ToUpperInvariant();
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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 sb = new StringBuilder();
|
||||
var dts = dt.Split(' ');
|
||||
sb.Append((char) int.Parse(dts[0]) + 65);
|
||||
sb.Append((char) int.Parse(dts[1]) + 65);
|
||||
sb.Append((char) int.Parse(dts[2]) + 97);
|
||||
sb.Append((char) int.Parse(dts[3]) + 65);
|
||||
sb.Append((char) int.Parse(dts[4]) + 97);
|
||||
sb.Append((char) int.Parse(dts[5]) + 97);
|
||||
sb.Append((char)int.Parse(dts[0]) + 65);
|
||||
sb.Append((char)int.Parse(dts[1]) + 65);
|
||||
sb.Append((char)int.Parse(dts[2]) + 97);
|
||||
sb.Append((char)int.Parse(dts[3]) + 65);
|
||||
sb.Append((char)int.Parse(dts[4]) + 97);
|
||||
sb.Append((char)int.Parse(dts[5]) + 97);
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
|
|
103
QueryHelper.cs
Normal file
103
QueryHelper.cs
Normal 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<TModel>.</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<TModel>.</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<TModel>.</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);
|
||||
}
|
||||
}
|
||||
}
|
6
Squid.cs
6
Squid.cs
|
@ -47,7 +47,7 @@ namespace FCS.Lib
|
|||
/// <summary>
|
||||
/// A read-only object of the Squid struct.
|
||||
/// Value is guaranteed to be all zeroes.
|
||||
/// Equivalent to <see cref="Guid.Empty" />.
|
||||
/// Equivalent to Guid.Empty" />.
|
||||
/// </summary>
|
||||
public static readonly Squid Empty = new(Guid.Empty);
|
||||
|
||||
|
@ -129,12 +129,12 @@ namespace FCS.Lib
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialises a new object of the Squid using <see cref="Guid.NewGuid()" />.
|
||||
/// Initialises a new object of the Squid using Guid.NewGuid()" />.
|
||||
/// </summary>
|
||||
/// <returns>New Squid object</returns>
|
||||
public static Squid NewGuid()
|
||||
{
|
||||
return new(Guid.NewGuid());
|
||||
return new Squid(Guid.NewGuid());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Loading…
Reference in a new issue