Link to home
Start Free TrialLog in
Avatar of maqskywalker
maqskywalker

asked on

converting VB function with linq query to C# function

I have a ASP.NET MVC application.  I'm using Visual Studio 2013.

I'm using VB.
For my data model I created a model called Northwind.dbml
In that model I'm using the Employees table from the Northwind database.

So in my controller I have a function that looks like this:
This function works just fine.

        ' To Get JSON Data 
        Public Function SelectNorthwindData(ByVal sidx As String, ByVal sord As String, ByVal page As Integer, ByVal rows As Integer) As ActionResult
            Dim context As New NorthwindDataContext
            Dim pageIndex As Integer = Convert.ToInt32(page) - 1
            Dim pageSize As Integer = rows
            Dim totalRecords As Integer = context.Employees.Count()
            Dim totalPages As Integer = CInt(Math.Ceiling(CSng(totalRecords) / CSng(pageSize)))

            Dim jsonData = New With { _
                .total = totalPages, _
                .page = page, _
                .records = totalRecords, _
                .rows = (From p In context.Employees _
                         Order By (p.LastName & " " & sord) _
                    Select New With {.id = p.EmployeeID, .cell = {p.EmployeeID, p.LastName, p.FirstName, p.Title}}).ToArray()}

            Return Json(jsonData, JsonRequestBehavior.AllowGet)
        End Function

Open in new window


So now I'm creating the C# version of the same application.
I'm having trouble converting this VB function shown above to C#.

This is what i have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml;
using NorthwindAppCS.Models;

namespace NorthwindAppCS.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }


        public ActionResult SelectNorthwindData(string sidx, string sord, int page, int rows)
        {
            NorthwindDataContext context = new NorthwindDataContext();
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = context.Employees.Count();
            int totalPages = Convert.ToInt32(Math.Ceiling(Convert.ToSingle(totalRecords) / Convert.ToSingle(pageSize)));

            dynamic jsonData = new {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = (from p in context.Employees 
                        orderby (p.LastName + " " + sord) 
                        new {
                             id = p.EmployeeID
                            ,cell = {p.EmployeeID, p.LastName, p.FirstName, p.Title 
                            }
		                           }).ToArray()
	        };

	return Json(jsonData, JsonRequestBehavior.AllowGet);
}


    }
}

Open in new window


If you look at the picture, the parts towards the end of my function have a little red mark, I'm getting those as wrong.

User generated image

Anyone know where I'm making a mistake in translating the VB function above  to C#?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial