public static IEnumerable <TEntity> Select <TEntity>(this Expression <Func <TEntity, object> > selectedColumns, Expression <Func <TEntity, bool> > where = null) where TEntity : class, new() { TEntity CreateInstance(System.Reflection.PropertyInfo[] properties, DataRow row, IEnumerable <string> columns) { TEntity entity = new TEntity(); foreach (var property in properties) { if (columns.Contains(property.Name)) { property.SetValue(entity, row.GetField <object>(property.Name)); } } return(entity); } return(Invoke(db => { var table = db.GetTable <TEntity>(); List <string> _columns = new PropertyTranslator().Translate(selectedColumns); string _where = new QueryTranslator().Translate(where); string SQL = table.SelectFromWhere(_where, _columns); var dt = db.FillDataTable(SQL); if (dt == null || dt.Rows.Count == 0) { return new List <TEntity>(); } var properties = typeof(TEntity).GetProperties(); return dt.ToList(row => CreateInstance(properties, row, _columns)); })); }
/// <summary> /// Update rows /// </summary> /// <param name="entity"></param> /// <param name="modifiedProperties">The properties are modified</param> /// <param name="where"></param> public void PartialUpdateOnSubmit(TEntity entity, Expression <Func <TEntity, object> > modifiedProperties, Expression <Func <TEntity, bool> > where) { if (entity == null) { throw new ArgumentNullException($"argument {nameof(entity)} cannot be null"); } List <string> names = new PropertyTranslator().Translate(modifiedProperties); string _where = new QueryTranslator().Translate(where); var gen = new SqlColumnValuePairCollection(); foreach (var propertyInfo in entity.GetType().GetProperties()) { if (names.IndexOf(propertyInfo.Name) == -1) { continue; } object value = propertyInfo.GetValue(entity); gen.Add(propertyInfo.Name, value); } SqlTemplate template = new SqlTemplate(formalName); string update = template.Update(gen.Join(","), _where); Context.CodeBlock.AppendLine <TEntity>(update); var evt = new RowEvent { TypeName = typeof(TEntity).Name, Operation = RowOperation.PartialUpdate, Row = gen.ToDictionary(), }; Context.RowEvents.Add(evt); gen.Clear(); return; }