public static IQueryable <T> OfStockKey <T>(this IQueryable <T> q, StockKeyBase stockKey) where T : class, IHasStockKey { if (q == null) { throw new ArgumentNullException(nameof(q)); } if (stockKey == null) { throw new ArgumentNullException(nameof(stockKey)); } var components = GetComponents(); string where = BuildWhereClause(components); q = q.Where(where, components.Select(x => x.ComponentValue).ToArray()); return(q); List <StockKeyComponent> GetComponents() { return(stockKey.GetType() .GetProperties() .Select(x => new StockKeyComponent(x.Name, x.GetValue(stockKey))) .ToList()); } }
private static StockKeyBase GetStockKey(this IHasStockKey hasStockKey, Type stockKeyType) { if (hasStockKey == null) { throw new ArgumentNullException(nameof(hasStockKey)); } if (stockKeyType == null) { throw new ArgumentNullException(nameof(stockKeyType)); } if (typeof(StockKeyBase).IsAssignableFrom(stockKeyType) == false) { throw new ArgumentException("应为 StockKey 的子类", nameof(stockKeyType)); } var entityProps = hasStockKey.GetType() .GetProperties(BindingFlags.Public | BindingFlags.Instance) .ToArray(); List <object> values = new List <object>(); foreach (var keyParam in stockKeyType.GetConstructors()[0].GetParameters()) { var entityProp = entityProps.SingleOrDefault(x => x.Name == keyParam.Name); if (entityProp == null) { throw new InvalidOperationException($"未找到属性,类型【{hasStockKey.GetType()}】,名称【{keyParam.Name}】"); } object value = entityProp.GetValue(hasStockKey); values.Add(value); } StockKeyBase stockKey = (StockKeyBase)Activator.CreateInstance(stockKeyType, values.ToArray()); return(stockKey); }
public string GetOutOrdering(StockKeyBase stockKey) { return(((DefaultStockKey)stockKey).Batch); }