Hi,

having code placed below, I cannot clue out, how to convert query in FilterData() to a dynamic query. Any ideas how to do it? Thanks in advance for any hint.

Martin

public class Item
{
    public DateTime SampleDate;  
    public List<String> Columns = new List<String>(); //this stores all columns
}

public class ItemList : List<Item>
{
    public void GetDataFromFile()
    {
        //hier I read data line by line from a file, and split each line to get column
        foreach(var row in "rows read from a file")
        {
            Item myRow = new Item();
            myRow.SampleDate = "data column read from a file";
            foreach(var col in "columns read from a file")
            {
                myRow.Columns.Add("col");
            }
            this.Add(myRow)
        }
    }
    public void FilterData(string criteria)
    {
        var query = this.Where(x => ((Item)x).Columns[3] == criteria).ToList();
    }
}

Recommended Answers

All 4 Replies

Are you

using System.Linq.Dynamic;

assuming it exists in your VS. It does not exists in my VS2012 (as far as I have checked). You can download it from here : Click Here

Hi,

thanks for your reply. Would it be also possible to biuld this query dynamically without this library? As I understand it is an extern library and I am not sure if I am allowed to freely use such in my project.

They give you the csharp code, so you just integrate it in your code no? Did not use it myself and I'm not sure you can freely use it, although if you have the code, I don't see how you can be prevent you from using it.

Hi,

it's about licensing and procedures in my company... Another reason would be why I would opt for not using external library is, that I am curious how to do this using Expression trees.
So far I have following things:

ParameterExpression param = Expression.Parameter(typeof(Item), "x");
MemberExpression left = Expression.PropertyOrField(param, "Columns");           
List<string> listTest = new List<string>();
listTest.Add(criteria);
ConstantExpression consToCompare = Expression.Constant(listTest, typeof(List<string>)); //???
var body = Expression.Equal(left, consToCompare);
var lambda = Expression.Lambda<Func<Item, bool>>(body, param);

This creates

lambda = {x => (x.Columns = value(System.Collections.Generic.List`1[System.String]))}

but I need

lambda = {x => (x.Columns[3] = value(System.Collections.Generic.List`1[System.String]))}

Do you have any idea how can I get Columns[3] to lambda?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.