Solved

converting VB function with linq query to C# function

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2013 debugging 2 35
Memory Usage 2 50
Parse the OuterHTML Data in the txtfile using c# 4 48
Entity Framework 7 32
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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, f…

920 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

15 Experts available now in Live!

Get 1:1 Help Now