if I'm passing in a string column name, how do I call a distinct list of items by property name?
private myEntities db = new myEntities();
...
//function passes in string
var vals = db.myEntityClass
.Select(v => v.????).Distinct() //I want this to be selected dynamically
If you are using .NET 4.0, here's a post by David Fowler that makes use of the new dynamic type feature to create a DynamicQueryable and DynamicExpressionBuilder which allows you to reference entity properties dynamically.
Or.. if you rather get straight to it, he's also created a library http://bitbucket.org/dfowler/dynamiclinq the encapsulates the functionality. It's also on NuGet :)
One thing you can do is use an extension method to get the property I wrote a quick example but you will need to add additional sanity checks for your data but this is the base case.
static class BadIdea
{
public static Typ GetValue<Typ>(this object o, string PropName)
{
Type T = o.GetType();
Typ ret = default(Typ);
System.Reflection.PropertyInfo pi = T.GetProperty(PropName);
if (pi != null)
{
object tempRet = pi.GetValue(o, new object[] { });
ret = (Typ)Convert.ChangeType(tempRet, ret.GetType());
}
else
{
return default(Typ);
}
return ret;
}
public class Tst
{
public int A { get; set; }
public int B { get; set; }
}
static void Main(string[] args)
{
List<Tst> vals =new List<Tst>() { new Tst() { A = 4, B = 6 }, new Tst() { A = 4, B = 7 } };
var lst = vals.Where((x) => x.GetValue<int>("A") == 4);
foreach (Tst ot in lst)
{
Console.WriteLine("A : {0} ; B: {1}", ot.A, ot.B);
}
}
You can try this
(p => p.GetType().GetProperty(exp).GetValue(p, null)).ToString();
Related
If I have an Enumerable of objects and want to modify multiple fields of a single one that I already know the index, I currently do:
var myObject = myEnumerable[index];
myObject.one = 1;
myObject.two = 2;
Is there a way to compact that? To make it simpler?
As an example, in VB you can do:
With myEnumerable[index]
.one = 1
.two = 2
End With
PS: using doesn't work here as the object would need to implement IDisposable, we don't' always control the object. I'm looking for a generic way to do this.
Handmade way of obtaining what you wanted. You can use it on every object.
class Program
{
static void Main(string[] args)
{
var item = new Item
{
One = 0,
Two = 0
};
item.SetProperties(new string[] { "One", "Two" }, new object[] { 1, 2 });
}
}
public class Item
{
public int One { get; set; }
public int Two { get; set; }
}
public static class Extensions
{
public static void SetProperties<T>(this T obj, IEnumerable<string> propertiesNames, IEnumerable<object> propertiesValues)
{
if (propertiesNames.Count() != propertiesValues.Count())
{
throw new ArgumentNullException();
}
var properties = obj.GetType().GetProperties();
for (int i = 0; i < propertiesNames.Count(); i++)
{
var property = properties.FirstOrDefault(x => x.Name == propertiesNames.ElementAt(i));
if (property is null)
{
throw new ArgumentException();
}
property.SetValue(obj, propertiesValues.ElementAt(i));
}
}
public static void SetFields<T>(this T obj, IEnumerable<string> fieldsNames, IEnumerable<object> fieldsValues)
{
if (fieldsNames.Count() != fieldsValues.Count())
{
throw new ArgumentNullException();
}
var fields = obj.GetType().GetFields();
for (int i = 0; i < fieldsNames.Count(); i++)
{
var field = fields.FirstOrDefault(x => x.Name == fieldsNames.ElementAt(i));
if (field is null)
{
throw new ArgumentException();
}
field.SetValue(obj, fieldsValues.ElementAt(i));
}
}
}
I get an exception when I try to set a nested member Property using FastMember. For example when having these classes
public class A
{
public B First { get; set; }
}
public class B
{
public string Second { get; set; }
}
and I want to set First.Second of an instance to "hello".
var b = new B{ Second = "some value here" };
var a = new A{ First = b };
var accessor = ObjectAccessor.Create(a);
accessor["First.Second"] = value; // this does not work and gives ArgumentOutOfRangeException
I can't split it up into ["First"]["Second"] because I don't know the depth at this point. Is there a magical access for nested properties or do I have to split the hierarchy myself?
I solved the problem recursively using an Extension Method this way:
public static class FastMemberExtensions
{
public static void AssignValueToProperty(this ObjectAccessor accessor, string propertyName, object value)
{
var index = propertyName.IndexOf('.');
if (index == -1)
{
accessor[propertyName] = value;
}
else
{
accessor = ObjectAccessor.Create(accessor[propertyName.Substring(0, index)]);
AssignValueToProperty(accessor, propertyName.Substring(index + 1), value);
}
}
}
... and this is started as follows:
ObjectAccessor.Create(a).AssignValueToProperty("First.Second", "hello")
You need to traverse the object graph using multiple ObjectAccessor instances.
public static void UseFastMember()
{
var b = new B { Second = "some value here" };
var a = new A { First = b };
var value = "hello";
var a_accessor = ObjectAccessor.Create(a);
var first = a_accessor["First"];
var b_accessor = ObjectAccessor.Create(first);
b_accessor["Second"] = value;
}
Hats off to #Beachwalker for the inspiration. But should you be using TypeAccessor as opposed to ObjectAccessor this is an extension method I've had much success with:
public static class TypeAccessorExtensions
{
public static void AssignValue<T>(this TypeAccessor accessor, T t, MemberSet members, string fieldName, object fieldValue)
{
var index = fieldName.IndexOf('.');
if (index == -1)
{
if (members.Any(m => string.Equals(m.Name, fieldName, StringComparison.OrdinalIgnoreCase)))
accessor[t, fieldName] = fieldValue;
}
else
{
string fieldNameNested = fieldName.Substring(0, index);
var member = members.FirstOrDefault(m => string.Equals(m.Name, fieldNameNested, StringComparison.OrdinalIgnoreCase));
if (member != null)
{
var nestedAccesor = TypeAccessor.Create(member.Type);
var tNested = accessor[t, fieldNameNested];
if (tNested == null)
{
tNested = Activator.CreateInstance(member.Type);
accessor[t, fieldNameNested] = tNested;
}
nestedAccesor.AssignValue(tNested, nestedAccesor.GetMembers(), fieldName.Substring(index + 1), fieldValue);
}
}
}
}
I am using VS2010 and EF4.0. The goal is to select fields of any IEnumerable, in order to show in the DataGridView. Take Northwind.Employees as example, the following code is OK.
private void button1_Click(object sender, EventArgs e)
{
NorthwindEntities en = new NorthwindEntities();
dataGridView1.DataSource = SelectNew(en.Employees, new string[] { "EmployeeID", "FirstName" });
}
public object SelectNew(object items, string[] fields)
{
IEnumerable<Employee> ems = items as IEnumerable<Employee>;
return ems.Select(em => new
{
id = em.EmployeeID,
name = em.FirstName
}
).ToArray();
}
The parameter object items is IEnumerable of EntityObject, and the function will be executed at client side memorry and shall have nothing to do with database now.
But I don't know the EntityObject type (Employee) until runtime, so maybe some complex reflection will be used.
I have checked this,
but when I bind the result to the control, it showed only blank rows without any column or data. And the funciton is for IQueryable, I have tried IEnumerable.AsQueryable and pass to it, but the results did not show any column either.
I've modified the example I pointed to in my comment above. This actually returns an IEnumerable<Dictionary<string,object>>, where each Dictionary represents one of the "new objects", and each key value pair in the dictionary represents a property and its value. Perhaps you can modify this for your use?
I'm not sure if you can simply bind the result to the DataGrid, but you should be able to figure it out.
I don't believe it's possible to create an anonymous type on the fly... But it might be possible to change this to use a dynamic type like ExpandoObject instead of a Dictionary. See this question for some hints on how to do that. I've never used dynamic objects, so you're on your own there!
public class TestClassA {
public string SomeString { get; set; }
public int SomeInt { get; set; }
public TestClassB ClassB { get; set; }
}
public class TestClassB {
public string AnotherString { get; set; }
}
public class Program {
private static void Main(string[] args) {
var items = new List<TestClassA>();
for (int i = 0; i < 9; i++) {
items.Add(new TestClassA {
SomeString = string.Format("This is outer string {0}", i),
SomeInt = i,
ClassB = new TestClassB { AnotherString = string.Format("This is inner string {0}", i) }
});
}
var newEnumerable = SelectNew(items, new string[] { "ClassB.AnotherString" });
foreach (var dict in newEnumerable) {
foreach (var key in dict.Keys)
Console.WriteLine("{0}: {1}", key, dict[key]);
}
Console.ReadLine();
}
public static IEnumerable<Dictionary<string, object>> SelectNew<T>(IEnumerable<T> items, string[] fields) {
var newItems = new List<Dictionary<string, object>>();
foreach (var item in items) {
var dict = new Dictionary<string, object>();
foreach (var field in fields)
dict[field] = GetPropertyValue(field, item);
newItems.Add(dict);
}
return newItems;
}
private static object GetPropertyValue(string property, object o) {
if (property == null)
throw new ArgumentNullException("property");
if (o == null)
throw new ArgumentNullException("o");
Type type = o.GetType();
string[] propPath = property.Split('.');
var propInfo = type.GetProperty(propPath[0]);
if (propInfo == null)
throw new Exception(String.Format("Could not find property '{0}' on type {1}.", propPath[0], type.FullName));
object value = propInfo.GetValue(o, null);
if (propPath.Length > 1)
return GetPropertyValue(string.Join(".", propPath, 1, propPath.Length - 1), value);
else
return value;
}
}
So I am writing a C# application, using .net/c# 4.0
I have a method which takes in a custom type and a dictionary.
I reuse this for a variety of things but for some reason I cannot think of a way to encapsulate the logic. The problem is this line
if (FastIntParse.FastParse(_dict[_Rule.Key].hourly_data[a].PropertyA) >
_Rule.Value)
In another use it may be
if (FastIntParse.FastParse(_dict[_Rule.Key].hourly_data[a].PropertyB) >
_Rule.Value)
The only thing that varies in the various cases is the Property I am using to compare to the rule value. For some reason I cannot think of a way to reuse it because I don't have the value to pass in to some function since the value is derived IN the function. How can I write a function to abstract away it's need to know which value it needs to derive and pass that information in ie pass it which property it will need to check and not the value of said property.
int a;
for (int z= 0;z<=2;z++)
{
a = (z * z) * 24;
for (; (a%24) <= _Rule.AlertEndTime; a++)
{
if (FastIntParse.FastParse(_dict[_Rule.Key].hourly_data[a].PropertyA) >
_Rule.Value)
{
EnqueueRuleTrigger(_Rule);
break;
}
}
}
I keep rewriting this method inline wherever I need it with the proper property.... this is obviously quite wasteful and any change needs to be made in many places.
Thanks in advance
You can use an Expression and then pull out the property within the method, then use reflection to tie this up to the object within the method
class Program
{
static void Main(string[] args)
{
List<PropertyBag> bags = new List<PropertyBag>()
{
new PropertyBag() {Property1 = 1, Property2 = 2},
new PropertyBag() {Property1 = 3, Property2 = 4}
};
Runme(x => x.Property1, bags);
Runme(x => x.Property2, bags);
Console.ReadLine();
}
public static void Runme(Expression<Func<PropertyBag, int>> expression, List<PropertyBag> bags)
{
var memberExpression = expression.Body as MemberExpression;
var prop = memberExpression.Member as PropertyInfo;
bags.ForEach( bag =>
Console.WriteLine(prop.GetValue(bag, null))
);
}
}
public class PropertyBag
{
public int Property1 { get; set; }
public int Property2 { get; set; }
}
}
to solve the problem with access to different properties and with the use of different boolean-function (<, >, ==) you could use delegates like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
delegate bool CompareFunction(Fii test, Foo item);
class Program
{
static List<Foo> list = new List<Foo>() {
new Foo() { PropertyA = 0, PropertyB = 9 },
new Foo() { PropertyA = 1, PropertyB = 10 }
};
static Fii test = new Fii() { PropertyA = 1 };
static void Main(string[] args)
{
Bar(list, delegate(Fii item1, Foo item2) { return item2.PropertyA < item1.PropertyA; });
Bar(list, delegate(Fii item1, Foo item2) { return item2.PropertyB > item1.PropertyA; });
Bar(list, delegate(Fii item1, Foo item2) { return item2.PropertyA == item1.PropertyA; });
Console.ReadLine();
}
static void Bar(List<Foo> list, CompareFunction cmp)
{
foreach (Foo item in list)
if (cmp(test, item))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
class Foo
{
public int PropertyA { get; set; }
public int PropertyB { get; set; }
}
class Fii
{
public int PropertyA { get; set; }
}
}
Make your function take a lambda argument and pass it _ => _.PropertyA, _ => _.PropertyB etc.:
void CheckAndEnqueueRulesByProperty (Func<YourObject, string> propertyGetter)
{
...
if (FastIntParse.FastParse (propertyGetter (
_dict[_Rule.Key].hourly_data[a])) > _Rule.Value)
{
...
}
...
}
If you have many types of objects to check with the same logic, make this function generic.
F# has a convenient feature "with", example:
type Product = { Name:string; Price:int };;
let p = { Name="Test"; Price=42; };;
let p2 = { p with Name="Test2" };;
F# created keyword "with" as the record types are by default immutable.
Now, is it possible to define a similar extension in C#?
seems it's a bit tricky, as in C# i'm not sure how to convert a string
Name="Test2"
to a delegate or expression?
public static T With<T, U>(this T obj, Expression<Func<T, U>> property, U value)
where T : ICloneable {
if (obj == null)
throw new ArgumentNullException("obj");
if (property == null)
throw new ArgumentNullException("property");
var memExpr = property.Body as MemberExpression;
if (memExpr == null || !(memExpr.Member is PropertyInfo))
throw new ArgumentException("Must refer to a property", "property");
var copy = (T)obj.Clone();
var propInfo = (PropertyInfo)memExpr.Member;
propInfo.SetValue(copy, value, null);
return copy;
}
public class Foo : ICloneable {
public int Id { get; set; }
public string Bar { get; set; }
object ICloneable.Clone() {
return new Foo { Id = this.Id, Bar = this.Bar };
}
}
public static void Test() {
var foo = new Foo { Id = 1, Bar = "blah" };
var newFoo = foo.With(x => x.Bar, "boo-ya");
Console.WriteLine(newFoo.Bar); //boo-ya
}
Or, using a copy constructor:
public class Foo {
public Foo(Foo other) {
this.Id = other.Id;
this.Bar = other.Bar;
}
public Foo() { }
public int Id { get; set; }
public string Bar { get; set; }
}
public static void Test() {
var foo = new Foo { Id = 1, Bar = "blah" };
var newFoo = new Foo(foo) { Bar = "boo-ya" };
Console.WriteLine(newFoo.Bar);
}
And a slight variation on George's excellent suggestion, that allows for multiple assignments:
public static T With<T>(this T obj, params Action<T>[] assignments)
where T : ICloneable {
if (obj == null)
throw new ArgumentNullException("obj");
if (assignments == null)
throw new ArgumentNullException("assignments");
var copy = (T)obj.Clone();
foreach (var a in assignments) {
a(copy);
}
return copy;
}
public static void Test() {
var foo = new Foo { Id = 1, Bar = "blah" };
var newFoo = foo.With(x => x.Id = 2, x => x.Bar = "boo-ya");
Console.WriteLine(newFoo.Bar);
}
I would probably use the second one since (1) any general purpose solution is going to be unnecessarily slow and convoluted; (2) it has the closest syntax to what you want (and the syntax does what you expect); (3) F# copy-and-update expressions are implemented similarly.
Maybe something like this:
void Main()
{
var NewProduct = ExistingProduct.With(P => P.Name = "Test2");
}
// Define other methods and classes here
public static class Extensions
{
public T With<T>(this T Instance, Action<T> Act) where T : ICloneable
{
var Result = Instance.Clone();
Act(Result);
return Result;
}
}
As an alternative to lambda function, you can use parameters with default values. The only minor issue is that you have to pick some default value that means do not change this parameter (for reference types), but null should be a safe choice:
class Product {
public string Name { get; private set; }
public int Price { get; private set; }
public Product(string name, int price) {
Name = name; Price = price;
}
// Creates a new product using the current values and changing
// the values of the specified arguments to a new value
public Product With(string name = null, int? price = null) {
return new Product(name ?? Name, price ?? Price);
}
}
// Then you can write:
var prod2 = prod1.With(name = "New product");
You have to define the method yourself, but that's always the case (unless you're going to use reflection, which less efficient). I think the syntax is reasonably nice too. If you want to make it as nice as in F#, then you'll have to use F# :-)
There is no native ability to do this in C# short of an extension method, but at what cost? a and b are reference types and any suggestion that b is based ("with") on a causes immediate confusion as to how many objects we are working with. Is there only one? Is b a copy of a ? Does b point to a ?
C# is not F#.
Please see a previous SO question of mine as answered by Eric Lippert:
"Amongst my rules of thumb for writing clear code is: put all side effects in statements; non-statement expressions should have no side effects."
More fluent C# / .NET