Solved

converting VB function with linq query to C# function

Posted on 2014-09-09
1
121 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
1 Comment
 
LVL 74

Accepted Solution

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now