Linq dynamic query - how do I build a select clause?

I am trying to run some tests with Dynamic Linq at the moment, but being new to this, I am having problems.

Currently, I have one DataTable that I populated with an SQL query in the database. Now I want to execute dynamic linq queries in this DataTable. This may seem counterintuitive, but my ultimate goal is to perform joins on two different data tables that are populated with two different databases, so keeping that in mind, I hope this makes more sense. I am trying to understand a simpler situation and experiment a bit with it before I continue this main problem.

The question is, I'm not quite sure about the choice between IQueryable (Of T) and IEnumerable (Of T). I understand that if you do everything in memory, you choose IEnumerable. I would think that this is suitable for my business, right? However, when I looked at changing IEnumerable to IQuerbyable (in the code snippet below), I was surprised to see that "x.Item (Field Name)" does not work !? What am I missing? The error he gave was: "Late Binding operations cannot be converted to an Expression Tree."

Anyway, let's see. I already got some of the work:

    Dim desc As String = "Description"
    Dim status As String = "Status"

    Dim query As IEnumerable(Of DataRow) = From x In tab.AsEnumerable()
    query = query.Where(Function(x As Object) x.Item(status) < 100)

    For Each row As DataRow In query.ToList()
    'Do something
    Next row

, . , , . , "x! " - . , .. . , :

query = query.Select(Of String)(Function(x As Object) x.Item(desc))

(N.B.: Select (Of String), . )

InvalidCastException: " " WhereSelectEnumerableIterator 2[System.Data.DataRow,System.String]' to type 'System.Collections.Generic.IEnumerable 1 [System.Data.DataRow] ". , ; , - , DataRow, , . - / ?

!

, , , , : where select. , , , . , .Where() .Select(), IEnumerable.

+5
1

query IEnumerable(Of DataRow) :

Dim query As IEnumerable(Of DataRow) = From x In tab.AsEnumerable()

IEnumerable(Of String) Select, .

, IEnumerable(Of String), query, IEnumerable(Of DataRow).

query = query.Select(Of String)(Function(x As Object) x.Item(desc))

IEnumerable(Of String) IEnumerable(Of DataRow) , InvalidCastException.


, , Function(x As Object) Where Select, Function(row As DataRow), , DataRow.

, :

Dim desc As String = "Description"
Dim status As String = "Status"

Dim columnToUse = status

Dim query = From x In tab.AsEnumerable()
            Where x.Item(status) < 100
            Select x(columnToUse)

For Each item as String In query.ToList()
    'Do something
Next 

columnToUse , .

, , ExpandoObjects Tuples - .

:

Dim columnToUse = new String() {desc, status} ' select columns dynamically

Dim query = tab.AsEnumerable().Where(Function(row) row.Item(status) < 100)

' Selecting dynamically
Dim result1 = query.Select(function(row) columnToUse.ToDictionary(function(c) c, function(c) row(c)))
Dim result2 = query.Select(Function(row)
                                Dim exp As IDictionary(Of String, Object) = new ExpandoObject()
                                For Each column in columnToUse
                                    exp(column) = row(column)
                                Next
                                return exp
                            End Function)
+3

All Articles