Tag Archives: LINQ

Get indexed collection out of LinqDataSource

I wanted to convert this SQLdatasource

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server”
ConnectionString=”<%$ ConnectionStrings:PaclikJConnectionString %>”
SelectCommand=”SELECT Row_number() OVER (ORDER BY Vlozeno DESC) as Poradi, ObrazID, Nazev, NazevAj, datepart(yy,DatumVyroby) as ‘RokVyroby’, Popis, PopisAj, cast(RozmerX as varchar(3)) + ‘x’ + cast(RozmerY as varchar(3)) + ‘ cm’ as Rozmer, Cena, NaProdej, Vlozeno, Nazev + ‘, rok: ‘ + cast(datepart(yy,DatumVyroby) as varchar(4)) + ‘, ‘ + Popis + ‘, ‘ + cast(RozmerX as varchar(3)) + ‘x’ + cast(RozmerY as varchar(3)) + ‘ cm’ as AltText, Nazev + ‘, rok: ‘ + cast(datepart(yy,DatumVyroby) as varchar(4)) + ‘, ‘ + PopisAj + ‘, ‘ + cast(RozmerX as varchar(3)) + ‘x’ + cast(RozmerY as varchar(3)) + ‘ cm’ as AltTextAj FROM Obrazy inner join Techniky on Obrazy.Technika = Techniky.TechnikaID ORDER BY Vlozeno DESC”>
</asp:SqlDataSource>

to LinqDataSource. The importnat part on the SQLDatasource is of cource the SQL query which indexes the rows. Lower is the solution I came up with to get the same behaviour with LInqToSQL Linqdatasource

Protected Sub LinqDataSource1_Selecting(sender As Object, e As System.Web.UI.WebControls.LinqDataSourceSelectEventArgs) Handles LinqDataSource1.Selecting
        'handle selecting event if you want to return custom objects ie. dont need all props or need to create custom props

        Dim ctx As New PaclikDataContext

	    'ObjectTrackingEnabled = False 'cos I'm not going to update object, just show them
        ctx.ObjectTrackingEnabled = False
        Dim Obrazy = (ctx.Obrazs _
                     .OrderByDescending(Function(o) o.Vlozeno) _
                     .Select(Function(o) New With { _
                                 .ObrazId = o.ObrazID, _
                                 .Nazev = o.Nazev, _
                                 .NazevAj = o.NazevAj, _
                                 .RokVyroby = o.DatumVyroby.Year, _
                                 .Popis = o.Technika1.Popis, _
                                 .PopisAj = o.Technika1.PopisAj, _
                                 .Rozmer = o.RozmerX.ToString & " x " & o.RozmerY.ToString & " cm"})).ToList

        'I used .ToList 'cos I need the query to return something that implements IEnumerable(Of T)
        ' as AFaIK the overload of Select with the second index param can be used only on IEnumerable(Of T) not on IQueryable

			'notice the creation of Index property .Index = i
			e.Result = Obrazy.Select(Function(o, i) New With {.Index = i, _
                                                          .ObrazId = o.ObrazId, _
                                                          .Nazev = o.Nazev, _
                                                          .NazevAj = o.NazevAj, _
                                                          .RokVyroby = o.RokVyroby, _
                                                          .Popis = o.Popis, _
                                                          .PopisAj = o.PopisAj, _
                                                          .Rozmer = o.Rozmer _
                                                          })

    End Sub
End Class
Advertisements
smsagent

Scripts, tools and tips, mostly around Microsoft SCCM and EMS

To The Point

Anything about Technology and Business

Brian's Power Windows Blog

Microsoft in the Enterprise. Windows, Hyper-V, Exchange, SQL, and more!

PowerScripting Podcast

Shownotes and links for the PowerScripting Podcast, a podcast to help people learn Windows Powershell

Learn Powershell | Achieve More

What is this Powershell of which you speak?