calling multiple columns in a linq query

i'm working with a jQuery UI Autocomplete in a ASP.NET C# Web Forms application.

In my project I'm using the Employees table from the Northwind Sql Server 2008 Database.

I created a model called Northwind.edmx.

I have a page called Default.aspx.

So in the code on this page Default.aspx.cs
Right after the Page_Load method I have a method called GetEmployees that looks like this:


        protected void Page_Load(object sender, EventArgs e)
        {

        }
       
        // *********** method for jquery ui autocomplete ***********
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<string> GetEmployees(string pre)
        {
            List<string> employees = new List<string>();
            using (NorthwindEntities db = new NorthwindEntities())
            {
                employees = (from a in db.Employees
                             where a.LastName.StartsWith(pre)
                             select a.LastName).ToList();
            }
            return employees;
        }
      // *********** method for jquery ui autocomplete ***********

On my Default.aspx I'm calling this GetEmployees method and feeding that to my jquery ui autocomplete.
So in my autocomplete if I type a LastName, my autocomplete shows matching lastnames.
My autocomplete works fine using this method.

My Code for Default.aspx looks like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="NorthwindApp.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <link type="text/css" rel="stylesheet" href="Content/jquery-ui.css" />
    <script type="text/javascript" src="Scripts/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery.validate.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery.validate.unobtrusive.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery-ui-1.8.24.min.js"></script>

    <style type="text/css">
        body {
            font-family: Arial;
        }

        html {
            background-color: #ffffff;
        }

        .tablestyle1 {
            width: 750px;
            /*border: 1px solid #0000ff;*/
            font-family: Arial;
            font-size: 12px;
        }

        .HeaderCol {
            height: 80px;
            font-weight: bold;
            text-align: center;
        }

        .style1col1 {
            width: 100px !important;
            /*border: 1px solid #00ff90;*/
        }

        .style1col2 {
            width: 250px;
            text-align: center;
            /*border: 1px solid #ff0000;*/
        }

        .style1col3 {
            width: 200px;
            text-align: center;
            /*border: 1px solid #00ffff;*/
        }

        .style1col4 {
            width: 200px !important;
            text-align: center;
            /*border: 1px solid #a423d0;*/
        }

        input, textarea {
            width: 200px;
            height: 20px;
            margin: 0 0 0;
        }

    </style>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table class="tablestyle1">
            <tr>
                <td colspan="4" class="HeaderCol">jQuery UI Autocomplete (Northwind Employees Example)</td>
            </tr>
            <tr>
                <td class="style1col1"></td>
                <td class="style1col2">This is a jQuery Autocomplete</td>
                <td class="style1col3"></td>
                <td class="style1col4"></td>
            </tr>
            <tr>
                <td class="style1col1"></td>
                <td class="style1col2"><input type="text" id="LastName" placeholder="Please type a LastName" /></td>
                <td class="style1col3">
                    
                </td>
                <td class="style1col4"></td>
            </tr>
        </table>
    </div>
    <br />
    <p id="demo"></p>
    <p id="demo2"></p>
    <p id="demo3"></p>
            <div id="selectedValue">
            </div>

    </form>
</body>
</html>
<script type="text/javascript">

    $(document).ready(function () {
        // ------------------------

        // *********** jQuery UI AutoComplete ***********
        $("#LastName").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "Default.aspx/GetEmployees",
                    data: "{ 'pre': '" + request.term + "' }",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function (data) { return data; },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return {
                                value: item,
                                //value: item.LastName
                               //value: item.EmployeeID
                               //value: item.FirstName

                            }
                        }))
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            },
            minLength: 2,
            // ***** Select Event *****
            select: function (even, ui) {

                var a = ui.item.value;
                document.getElementById("demo").innerHTML = "The item you selected has this value: " + a;

            },

        });
        // *********** jQuery UI AutoComplete ***********

        // ------------------------
    });
</script>

Open in new window



so this is my question.. In my GetEmployees method I have this line where I have my question:

                employees = (from a in db.Employees
                             where a.LastName.StartsWith(pre)
                             select a.LastName).ToList();

I believe this line is saying select the lastname from the Employees entity model and convert it to a list.

Is it possible to call  EmployeeID, FirstName and LastName columns and have the GetEmployees method return all three of these columns instead of just LastName like it is doing now.

I'm not sure of the syntax to get the method to return all three of these columns as a list.
LVL 1
maqskywalkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Naitik GamitSoftware DeveloperCommented:
Your Current Linq Query return LastName of employees,LastName Start with pre.
if you want EmployeeID, FirstName and LastName columns then try as:

  employees = (from a in db.Employees
                             where a.LastName.StartsWith(pre)
                             select new {a.EmployeeID,a.FirstName ,a.LastName }.ToList();
0
Lokesh B RDeveloperCommented:
Hi,

You can use the following query.

Anonymous Types

employees = (from a in db.Employees
                             where a.LastName.StartsWith(pre)
                             select new 
                              { 
                                     EmployeeID = a.EmployeeID,
                                     FirstName = a.FirstName,
                                     LastName = a.LastName
                              }).ToList();

Open in new window

0
maqskywalkerAuthor Commented:
I revised the method per your suggestion. This is what I have.

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<string> GetEmployees(string pre)
        {
            List<string> employees = new List<string>();
            using (NorthwindEntities db = new NorthwindEntities())
            {
                employees = (from a in db.Employees
                             where a.LastName.StartsWith(pre)
                             select new
                             {
                                 EmployeeID = a.EmployeeID,
                                 FirstName = a.FirstName,
                                 LastName = a.LastName
                             }).ToList();
            }
            return employees;
        }

Open in new window


But i get this one error

error message
0
Lokesh B RDeveloperCommented:
Hi,

You are trying to return multiple values but the return type is List<string>. So you can return the Employee List with the required parameters.

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<Employee> GetEmployees(string pre)
        {
            List<Employee> employees = new List<Employee>();
            using (NorthwindEntities db = new NorthwindEntities())
            {
                employees = (from a in db.Employees
                             where a.LastName.StartsWith(pre)
                             select new Employee
                             {
                                 EmployeeID = a.EmployeeID,
                                 FirstName = a.FirstName,
                                 LastName = a.LastName
                             }).ToList();
            }
            return employees;
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.