What is wrong with this linq query?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim ds As DataSet
    ds = getData()
    Dim dt As DataTable = ds.Tables(0)
    Dim gridViewData = From r As DataRow In dt.Rows Select r.Item("foo"), r.Item("bar")
    GridView1.DataSource = gridViewData
    GridView1.DataBind()
End Sub

I just wrote the previous code, and I get the following compile-time error: "The name of a range variable can only be inferred from a simple or qualified name with no arguments." Why am I getting this error? How can I fix my code?

+3
source share
2 answers

EDIT: Okay, now I understand the problem. This is in projection. Try the following:

Dim gridViewData = From r As DataRow In dt.Rows _
                   Select Foo = r.Item("foo"), Bar = r.Item("bar")

In principle, he did not know what to name properties in a projection.

+6
source

Unfortunately, I don't know the VB syntax for this, so I just need to show C # and hope you can solve it:

from DataRow r in dt.Rows
select new 
{
     r.Item("Foo"),
     r.Item("bar")
}

: " ", , , . - :

from DataRow r in dt.Rows
select new 
{
     r.Count,
     r.NumRows
}

, Count NumRows, , , , Item, .

, :

from DataRow r in dt.Rows
select new 
{
     Foo = r.Item("Foo"),
     Bar = r.Item("bar")
}
+2

Source: https://habr.com/ru/post/1759899/


All Articles