5

How can I do something like this:

var result = db.MyTable.Where(x => x."MyProperty" == "Test" );

As you can see I want to access "MyProperty" but give the property name as a sting.

2

4 Answers 4

7

You could use reflection

x.GetType( ).GetProperty("MyProperty").GetValue( x, null ); 

although this might work, i wouldn't advise on doing this, why don't pass in your where clause as an expression like:

myMethod<T>(Expression<Func<T,bool>> where)

example after comment:

consider the following type:

you see there are three properties where the name is of type string and id is of type int. now if we wrap our database context in a service like this

public class MyTypeOfXService
{
    private DataDataContext Context;
    public MyTypeOfXService()
    {
        Context = new DataDataContext("example code");
    }

    public IQueryable<MyTypeOfX> GetTypeOfX(Expression<Func<MyTypeOfX, bool>> where)
    {
        return this.Context.MyTypeOfXes.Where(where);
    }
}

in our get method there is an Expression parameter that takes two generics, the first is our type x and the second is a boolean. The advantage of this approach is that we can abstract all the data context creation and only express an where clause in our code see final piece of code:

class Program
{
    static void Main(string[] args)
    {
        var service = new MyTypeOfXService();

        var queryById = service.GetTypeOfX((mytype) => mytype.Id == 1);
        var queryByName = service.GetTypeOfX((mytype) => mytype.Name == "MyName");
        var queryByName = service.GetTypeOfX((mytype) => mytype.Name == "MyName" && mytype.Id == 1);
    }
}

as you can see, we can build a where clause on any property or combination of properties.

Sign up to request clarification or add additional context in comments.

2 Comments

I'am using Linq To SQL so GetProperty() doesn't work. How to do this with an expression ?
i'll provide you with an example, give me 15 mins.
4

I think you can try this:

public static IQueryable<T> SortByPropertyName<T>(this IQueryable<T> queryable, string orderFieldName) where T : Entity
{
    var param = Expression.Parameter(typeof(T), typeof(T).Name);
    var orderExpression = Expression.Lambda<Func<T, object>>(Expression.Property(param, orderFieldName), param);
    return queryable.OrderBy(orderExpression);
}

It works fine for order.

Comments

0

I don't know how x is implemented, but if x has an indexer, then you could formulate your query like:

var result = db.MyTable.Where(x => x["MyProperty"] == "Test" );

see http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx for indexers in c#

Comments

0

You can use reflection to get it unless you are querying a database directly using LINQ to SQL.

Here is a sample of how to get property info using reflection:

class Program
{
    class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public byte Age { get; set; }
        public override string ToString()
        {
            return string.Format("{0}, {1} ({2})", LastName, FirstName, Age);
        }
    }

    static void Main(string[] args)
    {
        Person p1 = new Person() { FirstName = "Bill", LastName = "Johnson", Age = 34 };
        Person p2 = new Person() { FirstName = "Sally", LastName = "Jones", Age = 21 };
        Person p3 = new Person() { FirstName = "Jame", LastName = "Smith", Age = 28 };

        List<Person> people = new List<Person>(new Person[] { p1, p2, p3 });

        IEnumerable<Person> foundPeople = people.Where(p => p.GetType().GetProperty("LastName").GetValue(p,null).ToString().StartsWith("J"));

        foreach (Person person in foundPeople)
        {
            Console.WriteLine(person.ToString());
        }
        Console.ReadKey();
    }
}

Just be careful because this could lead to performance issues if you are querying a large amount data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.