Dariusz Jankowski

Hi,

I think about possibility to access in linq query "columns" in the following way:

Code Snippet

var result = from x in MyDataContext.GetTable()

where x["Property1"] == "someting1" && x["Property2"] == something2

select x;

or this:

Code Snippet

var result = from x in MyDataContext.GetTable()

orderby x["Property3"]

select x;

Is anyone know is it possible - current syntax does not allow for it but maybe there is another way to do it Maybe someone from Linq Team could answer Will the syntax is hard to implement in the RTM

I thnik It could help to implement things like:

1. column ordering in DataGridView when DataSource is Linq Table. After user click oncolumn "we" read the name of the column and invoke method to reorder list (... orderby x["ColumnName"] ) . I Think This is simpler than build own methods to implement interface IComparer.

2. Build simpler methods to return one value. For example:

Code Snippet

public object GetValue(int id, string columnName)

{

var result = from x in MyDataContext.MyTable

where x.Id == id

select new { x["ColumnName"] );

return result;

}

3. I think other examples everyone can multiply.

Thanks for any reply.

Regards.

Dariusz Jankowski



Re: LINQ Project General Parametrized select by property name. Sugestion for new syntax.

Dariusz Jankowski

Now I test .NET 3.5Beta2 and VisualStudio2008Beta2. In Linq to SQL queries we still don't have ability to use syntax which I described above but I found one possibility to resolve this for some time - maybe not for solutions. Here is an example:

Code Snippet

IEnumerable Select(IQueryable datasource, string parameter1)

{

var result = from x in datasource.AsEnumerable()

where x.GetType().GetProperty("Property1").GetValue(x, null).ToString().Equals(parameter1)

orderby x.GetType().GetProperty("Property1").GetValue(x, null).

select x;

return x;

}

The trick is to use .AsEnumerable() on datasource of type IQueryable, which instruct to run query locally so we use reflection to read value using parameter - not Intellisense. The article which helped is here: http://msdn2.microsoft.com/en-us/library/bb425822.aspx

Another way explain Mike Taulty on his blog: http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/06/15/9489.aspx

and Aaron Erickson: http://blog.magenic.com/blogs/aarone/archive/2007/05/11/LINQ-to-Objects-and-Bi_2D00_Directional-Binding.aspx

What do You think about it If You have other sugestions - please reply.

Dariusz Jankowski





Re: LINQ Project General Parametrized select by property name. Sugestion for new syntax.

Dariusz Jankowski

Because .NET language syntax does not allow to get in linq query property or field by its name, I created methods using Linq Expressions. I hope LINQ team will implement it in the future.

Code Snippet

static Expression<Func<T, bool>> WhereEqual<T>(string propertyOrFieldName, object propertyOrFieldValue)

{

ConstantExpression const1 = Expression.Constant(propertyOrFieldValue);

ParameterExpression item = Expression.Parameter(typeof(T), "expr");

MemberExpression prop = LambdaExpression.PropertyOrField(item, propertyOrFieldName);

BinaryExpression filter = Expression.Equal(prop, const1);

var expr = Expression.Lambda<Func<T, bool>>(filter, new ParameterExpression[] { item });

expr.Compile();

return expr;

}

static Expression<Func<TSource, TResult>> GetPropertyOrFieldByName<TSource, TResult>(string propertyOrFieldName)

{

ParameterExpression item = Expression.Parameter(typeof(TSource), "expr");

MemberExpression prop = LambdaExpression.PropertyOrField(item, propertyOrFieldName);

var expr = Expression.Lambda<Func<TSource, TResult>>(prop, new ParameterExpression[] { item });

expr.Compile();

return expr;

}

How to use it It's simple:

//Table_1: some table in my DataContext with columns: Id, Name

Expression<Func<Table_1, string>> le = GetPropertyOrFieldByName<Table_1, string>("Name");

Expression<Func<Table_1, bool>> le1 = WhereEqual<Table_1>("Name", "Dariusz Jankowski");

string result = myDataContext.Table_1s.Where(le1).Select(le); //result = "Dariusz Jankowski";





Re: LINQ Project General Parametrized select by property name. Sugestion for new syntax.

ben2004uk

Just wondering,

What advantage does this have over current way of querying a table

Why would you want to access the properies by their name and not as a property

Ben






Re: LINQ Project General Parametrized select by property name. Sugestion for new syntax.

Dariusz Jankowski

I have generic form to filter linq "tables" (queries). The form doesn't know model of Data. As a DataSource I pass IQueryable object based on Table<T> and as a result return IEnumerable<T>.

If form user choose column to filter and fill search text.

After user click "Search" - I filter data in grid.

The process end when user choose records from grid.

Now The form is able to filter all LINQ Tables.

Before this I was using my own method to convert linq table to DataTable and paased this DataTable as source of form.

Another example is dmbrubac's post : http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=2014573&SiteID=1

I hope I will use it in other scenarios.

If some see other advantages, so please share them here.

Darek.