prep the field for building the select statement next.
This commit is contained in:
parent
436bd5ab98
commit
30d1d9072b
@ -48,6 +48,7 @@ namespace PoweredSoft.DynamicLinq.Test
|
||||
B = t.NetSales
|
||||
});*/
|
||||
|
||||
/*
|
||||
var dynamicSyntax2 = TestData.Sales
|
||||
.AsQueryable()
|
||||
.GroupBy(t => t.Path("ClientId").Path("NetSales", "B"));
|
||||
@ -69,7 +70,7 @@ namespace PoweredSoft.DynamicLinq.Test
|
||||
Data = t.ToList()
|
||||
}).ToList();
|
||||
|
||||
int i = 0;
|
||||
int i = 0;*/
|
||||
|
||||
|
||||
/*
|
||||
@ -84,16 +85,16 @@ namespace PoweredSoft.DynamicLinq.Test
|
||||
Sales = t.ToList()
|
||||
});*/
|
||||
|
||||
|
||||
var dynamicSyntax2 = TestData.Sales
|
||||
.AsQueryable()
|
||||
.GroupBy(t => t.Path("ClientId"))
|
||||
.Select(t =>
|
||||
{
|
||||
t.PropertyFromKey("TheClientId", "ClientId");
|
||||
t.Key("TheClientId", "ClientId");
|
||||
t.Count("Count");
|
||||
// don't have to implement right away.
|
||||
t.Count("CountClientId", "ClientId", ConditionOperators.GreaterThan, 1);
|
||||
t.LongCount("LongCount");
|
||||
t.Sum("NetSales");
|
||||
t.Average("TaxAverage", "Tax");
|
||||
t.Average("Tax", "TaxAverage");
|
||||
t.ToList("Sales");
|
||||
});
|
||||
}
|
||||
|
@ -41,6 +41,16 @@ namespace PoweredSoft.DynamicLinq
|
||||
Descending
|
||||
}
|
||||
|
||||
public enum SelectTypes
|
||||
{
|
||||
Key,
|
||||
Count,
|
||||
LongCount,
|
||||
Sum,
|
||||
Average,
|
||||
ToList
|
||||
}
|
||||
|
||||
internal static class Constants
|
||||
{
|
||||
internal static readonly MethodInfo GroupByMethod = typeof(Queryable).GetMethods().First(t => t.Name == "GroupBy");
|
||||
|
@ -88,5 +88,15 @@ namespace PoweredSoft.DynamicLinq
|
||||
|
||||
return QueryableHelpers.GroupBy(query, type, groupBuilder.Parts, groupBuilder.Type, groupBuilder.EqualityComparerType);
|
||||
}
|
||||
|
||||
public static IQueryable Select(this IQueryable query, Action<SelectBuilder> callback)
|
||||
{
|
||||
var sb = new SelectBuilder();
|
||||
callback(sb);
|
||||
if (sb.Empty)
|
||||
throw new Exception("No select specified, please specify at least one select path");
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,112 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace PoweredSoft.DynamicLinq.Fluent
|
||||
{
|
||||
public class SelectPart
|
||||
{
|
||||
public string Path { get; set; }
|
||||
public string PropertyName { get; set; }
|
||||
public SelectTypes SelectType { get; set; }
|
||||
}
|
||||
|
||||
public class SelectBuilder
|
||||
{
|
||||
public List<SelectPart> Parts = new List<SelectPart>();
|
||||
|
||||
public bool Empty => Parts?.Count == 0;
|
||||
|
||||
protected void throwIfUsedOrEmpty(string propertyName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(propertyName))
|
||||
throw new ArgumentNullException($"{propertyName} cannot end up be empty.");
|
||||
|
||||
if (Parts.Any(t => t.PropertyName == propertyName))
|
||||
throw new Exception($"{propertyName} is already used");
|
||||
}
|
||||
|
||||
public SelectBuilder Key(string propertyName, string path = null)
|
||||
{
|
||||
if (propertyName == null)
|
||||
propertyName = path.Split('.').LastOrDefault();
|
||||
|
||||
throwIfUsedOrEmpty(propertyName);
|
||||
|
||||
Parts.Add(new SelectPart
|
||||
{
|
||||
Path = path == null ? "Key" : $"Key.{path}",
|
||||
PropertyName = propertyName,
|
||||
SelectType = SelectTypes.Key
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public SelectBuilder Count(string propertyName)
|
||||
{
|
||||
throwIfUsedOrEmpty(propertyName);
|
||||
Parts.Add(new SelectPart
|
||||
{
|
||||
PropertyName = propertyName,
|
||||
SelectType = SelectTypes.Count
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public SelectBuilder LongCount(string propertyName)
|
||||
{
|
||||
throwIfUsedOrEmpty(propertyName);
|
||||
Parts.Add(new SelectPart
|
||||
{
|
||||
PropertyName = propertyName,
|
||||
SelectType = SelectTypes.LongCount
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public SelectBuilder Sum(string path, string propertyName = null)
|
||||
{
|
||||
if (propertyName == null)
|
||||
propertyName = path.Split('.').LastOrDefault();
|
||||
|
||||
throwIfUsedOrEmpty(propertyName);
|
||||
|
||||
Parts.Add(new SelectPart
|
||||
{
|
||||
Path = path,
|
||||
PropertyName = propertyName,
|
||||
SelectType = SelectTypes.Sum
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public SelectBuilder Average(string path, string propertyName = null)
|
||||
{
|
||||
if (propertyName == null)
|
||||
propertyName = path.Split('.').LastOrDefault();
|
||||
|
||||
throwIfUsedOrEmpty(propertyName);
|
||||
|
||||
Parts.Add(new SelectPart
|
||||
{
|
||||
Path = path,
|
||||
PropertyName = propertyName,
|
||||
SelectType = SelectTypes.Average
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public SelectBuilder ToList(string propertyName)
|
||||
{
|
||||
throwIfUsedOrEmpty(propertyName);
|
||||
Parts.Add(new SelectPart
|
||||
{
|
||||
PropertyName = propertyName,
|
||||
SelectType = SelectTypes.ToList
|
||||
});
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user