I am trying to use the following form to enter new info into a database.
I am trying to use the entity framework.
I have the following classes of Interest:
public class InventoryContext : DbContext
{
public DbSet<Item> Items { get; set; }
....Other DbSets follow...
}
[Table("Items")]
public class Item
{
#region Strings
public string Color { get; set; }
public string FullName { get; set; }
[Column(@"Sheet/Roll")]
public string Type { get; set; }
public string PrimaryMachine { get; set; }
public string Alias { get; set; }
public string Brand { get; set; }
public string Finish { get; set; }
#endregion
#region Long
public long ID { get; set; }
public decimal? Weight { get; set; }
#endregion
#region Doubles
public decimal? Size1 { get; set; }
public decimal? Size2 { get; set; }
public decimal? Size3 { get; set; }
#endregion.
}
And I am stuck on filtering the dgvAllItems
, based on the selected drop down values. I think my code is ugly and there is a better way.
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox s = (ComboBox)sender;
IQueryable<Item> query = c.Items;
foreach (ComboBox cb2 in gbFilters.Controls.OfType<ComboBox>().Where(com => com.Text != ""))
{
string currentpropname = cb2.Name.Substring(2);
if (cb2.Name.Substring(2, 4) == "Size" || cb2.Name == "cbWeight")
{
decimal? currentpropvalue = Convert.ToDecimal(cb2.Text);
PropertyInfo propertyInfo = typeof(Item).GetProperty(currentpropname);
ParameterExpression pe = Expression.Parameter(typeof(Item), "e");
MemberExpression me = Expression.MakeMemberAccess(pe, propertyInfo);
ConstantExpression ce = Expression.Constant(currentpropvalue, typeof(decimal?));
BinaryExpression be = Expression.Equal(me, ce);
Expression<Func<Item, bool>> lambda = Expression.Lambda<Func<Item, bool>>(be, pe);
query = query.Where(lambda);
}
else
{
string currentpropvalue = cb2.Text;
PropertyInfo propertyInfo = typeof(Item).GetProperty(currentpropname);
ParameterExpression pe = Expression.Parameter(typeof(Item), "e");
MemberExpression me = Expression.MakeMemberAccess(pe, propertyInfo);
ConstantExpression ce = Expression.Constant(currentpropvalue, typeof(decimal?));
BinaryExpression be = Expression.Equal(me, ce);
Expression<Func<Item, bool>> lambda = Expression.Lambda<Func<Item, bool>>(be, pe);
query = query.Where(lambda);
}
}
bindingSource1.DataSource = query.ToList();
}
private void ComboBox_DropDown(object sender, EventArgs e)
{
ComboBox cb1 = (ComboBox)sender;
IQueryable<Item> query = c.Items;
foreach (ComboBox cb2 in gbFilters.Controls.OfType<ComboBox>().Where(com => com.Text != ""))
{
string currentpropname = cb2.Name.Substring(2);
if (cb2.Name.Substring(2, 4) == "Size" || cb2.Name == "cbWeight")
{
decimal? currentpropvalue = Convert.ToDecimal(cb2.Text);
PropertyInfo propertyInfo = typeof(Item).GetProperty(currentpropname);
ParameterExpression pe = Expression.Parameter(typeof(Item), "e");
MemberExpression me = Expression.MakeMemberAccess(pe, propertyInfo);
ConstantExpression ce = Expression.Constant(currentpropvalue, typeof(decimal?));
BinaryExpression be = Expression.Equal(me, ce);
Expression<Func<Item, bool>> lambda = Expression.Lambda<Func<Item, bool>>(be, pe);
query = query.Where(lambda);
}
else
{
string currentpropvalue = cb2.Text;
PropertyInfo propertyInfo = typeof(Item).GetProperty(currentpropname);
ParameterExpression pe = Expression.Parameter(typeof(Item), "e");
MemberExpression me = Expression.MakeMemberAccess(pe, propertyInfo);
ConstantExpression ce = Expression.Constant(currentpropvalue, typeof(decimal?));
BinaryExpression be = Expression.Equal(me, ce);
Expression<Func<Item, bool>> lambda = Expression.Lambda<Func<Item, bool>>(be, pe);
query = query.Where(lambda);
}
}
string ActivePropName = cb1.Name.Substring(2);
if (ActivePropName.Substring(0, 4) == "Size" || ActivePropName == "Weight")
{
ParameterExpression arg = Expression.Parameter(typeof(Item), "x");
Expression expr = Expression.Property(arg, ActivePropName);
LambdaExpression lambda = Expression.Lambda(expr, arg);
Expression<Func<Item, decimal?>> expression = (Expression<Func<Item, decimal?>>)lambda;
cb1.DataSource = query.Select(expression).Distinct().ToList();
}
else
{
ParameterExpression arg = Expression.Parameter(typeof(Item), "x");
Expression expr = Expression.Property(arg, ActivePropName);
LambdaExpression lambda = Expression.Lambda(expr, arg);
Expression<Func<Item, string>> expression = (Expression<Func<Item, string>>)lambda;
cb1.DataSource = query.Select(expression).Distinct().ToList();
}
}