开发者

How to convert the following lines to Generic LINQ statements

I have the following lines of code in C# that gets data using DataTables This is pretty generic and helps me with multiple tables.

object obj = ((DataRowView)editingElement.DataCon开发者_Python百科text).Row[this.SelectedValuePath];

I want to change this statement to a generic LINQ statement so that i can use it with multiple LINQ tables too.

Can somebody help me figure this out?


I don't think you can use LINQ to make the code you wrote nicer or more elegant in any way. I assume that the type of editingElement.DataContext is object, so you'll need to write the cast anyway. If you forget about the casting, your code is just indexed access:

var rows = (DataRowView)editingElement.DataContext;
object obj = rows.Row[this.SelectedValuePath]; 

LINQ doesn't have any features that would make indexing nicer, so I think this is the best you can get. One possible ugly thing is that you get object as the result and you'll need to cast that to some other type (e.g. CustomerInfo).

If you were using LINQ from the beginning (to populate the data for the DataContext), you could probably write something like this to access the customer:

var rows = (IEnumerable<CustomerInfo>)editingElement.DataContext;
CustomerInfo info = rows.Row[this.SelectedValuePath]; 

This would be a bit more elegant, because you'd need just a single cast. However, I think that your code is fine and LINQ cannot help you (in this piece of code).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜