Solved

converting VB function with linq query to C# function

Posted on 2014-09-09
1
128 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

732 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