Solved

converting VB function with linq query to C# function

Posted on 2014-09-09
1
126 Views
Last Modified: 2016-02-16
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.

temperror.png

Anyone know where I'm making a mistake in translating the VB function above  to C#?
0
Comment
Question by:maqskywalker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40313505
Put a select in front of the new in line 36.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

761 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question