rscg_queryables 2024.1111.1910
dotnet add package rscg_queryables --version 2024.1111.1910
NuGet\Install-Package rscg_queryables -Version 2024.1111.1910
<PackageReference Include="rscg_queryables" Version="2024.1111.1910" />
paket add rscg_queryables --version 2024.1111.1910
#r "nuget: rscg_queryables, 2024.1111.1910"
// Install rscg_queryables as a Cake Addin #addin nuget:?package=rscg_queryables&version=2024.1111.1910 // Install rscg_queryables as a Cake Tool #tool nuget:?package=rscg_queryables&version=2024.1111.1910
rscg_queryables
rscg_queryables
is a Roslyn Code Generator designed to generate extension methods for sorting and filtering IEnumerable
and IQueryable
collections based on a given class.
Sorting how the user wants in frontend - description
Consider a scenario where we need to display a list of Person
objects and allow the user to sort them by various properties. The user should have the ability to select the property and the sorting order.
public class Person
{
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public int Age { get; set; }
public string FullName
{
get
{
return $"{FirstName} {LastName}";
}
}
}
When data is transmitted over HTTP, it is often in the form of a string object. To sort by first name in descending order, the query string should look like this:
orderBy=FirstName&Asc=false
Then in the backend code we should parse the query string and apply the sorting logic.
if(queryString.ContainsKey("orderBy"))
{
string orderBy = queryString["orderBy"];
bool asc = queryString["asc"] == "false" ? false: true;//default is true
if(orderBy == "FirstName")
{
if(asc)
{
persons = persons.OrderBy(p => p.FirstName);
}
else
{
persons = persons.OrderByDescending(p => p.FirstName);
}
}
//do the same for other properties : LastName, Age, FullName
}
The solution
With rscg_queryables, you can do this in a more elegant way.
if(queryString.ContainsKey("orderBy"))
{
string orderBy = queryString["orderBy"];
bool asc = queryString["asc"] == "false" ? false: true;//default is true
persons = persons.OrderByAscDesc(orderBy, asc);
//or you can do this, if you want to control
//if(asc)
//{
// persons = persons.OrderBy(orderBy);
//}
//else
//{
// persons = persons.OrderByDescending(orderBy);
//}
}
This should be done for everything that implements IEnumerable or IQueryable.
Filtering Based on User Preferences - Description
Consider a scenario where we need to display a list of Person
objects and allow the user to filter them by various properties. The user should have the ability to select the property, the filter criteria, and the filter operator (equal or different).
When data is transmitted over HTTP, it is often in the form of a string object. To filter by first name where the value is "John", the query string should look like this:
filterBy=FirstName&filterOperator=equal&filterValue=John
In the backend code, we need to parse the query string and apply the appropriate filtering logic.
if (queryString.ContainsKey("filterBy") && queryString.ContainsKey("filterOperator") && queryString.ContainsKey("filterValue"))
{
string filterBy = queryString["filterBy"];
string filterOperator = queryString["filterOperator"];
string filterValue = queryString["filterValue"];
if (filterBy == "FirstName")
{
if (filterOperator == "equal")
{
persons = persons.Where(p => p.FirstName == filterValue);
}
else if (filterOperator == "different")
{
persons = persons.Where(p => p.FirstName != filterValue);
}
}
// Do the same for other properties: LastName, Age, FullName
}
The Solution
With rscg_queryables
, you can achieve this in a more elegant and efficient manner.
- add the nugets to your project
<ItemGroup>
<PackageReference Include="rscg_queryablesCommon" Version="2024.1111.1910" />
<PackageReference Include="rscg_queryables" Version="2024.1111.1910" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
Optional see the code generated
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
- Modify the
Person
class to add therscg_queryables
attribute.
[MakeSortable]
[MakeWhere]
public class Person
{
//same code as above, omitted for brevity
}
- Use the overloaded
Where
method to filter the collection based on the query string.
if (queryString.ContainsKey("filterBy") && queryString.ContainsKey("filterOperator") && queryString.ContainsKey("filterValue"))
{
string filterBy = queryString["filterBy"];
string filterOperator = queryString["filterOperator"] == "equal"?WhereOperator.Equal:WhereOperator.Different;
string filterValue = queryString["filterValue"];
persons = persons.Where(filterBy, filterOperator, filterValue);
}
This approach can be applied to any collection that implements IEnumerable
or IQueryable
.
Other Roslyn Code Generators
For more Roslyn Source Code Generators, visit RSCG Examples https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.11.0)
- RSCG_NameGenerator (>= 2024.11.11.1830)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2024.1111.1910 | 97 | 11/11/2024 |
2024.1110.1815 | 98 | 11/10/2024 |
2024.1110.1054 | 87 | 11/10/2024 |