none
Return LINQ value as List RRS feed

  • Question

  • I am writing a WCF REST service in which I am querying database using LINQ. Below is the code

       public List<Employees> GetAllDataSetEmployees()
            {
                List<Employees> employees = new List<Employees>();

                string connStr = @"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;";
               // DataTable dt = new DataTable();

                DataClasses1DataContext ds = new DataClasses1DataContext(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;");

                var result = from p in ds.Employees
                             join empter in ds.EmployeeTerritories on p.EmployeeID equals empter.EmployeeID
                             join ter in ds.Territories on empter.TerritoryID equals ter.TerritoryID
                             select new { p.EmployeeID, p.FirstName, ter.TerritoryDescription };

                List<Employees> emp =  result.ToList<Employees>(); // getting error at this line

                return emp;
            }

    The inter face code is

     [OperationContract]
            [WebGet(UriTemplate = "GetAllDataSetEmployees",
                ResponseFormat = WebMessageFormat.Json)]
            List<Employees> GetAllDataSetEmployees();

    while returning the value I get the error at this statement  List<Employees> emp =  result.ToList<Employees>();

    How to convert the var value to list.

    Please any help.

    • Moved by Kristin Xie Wednesday, March 30, 2016 9:37 AM Linq related
    Tuesday, March 29, 2016 6:11 PM

Answers

  • Your "result" query returns anonymous objects: https://msdn.microsoft.com/en-us/library/bb397696.aspx

    And you cannot store anonymous objects objects in a List<Employees>. You should select Employee objects:

    var result = from p in ds.Employees
                              join empter in ds.EmployeeTerritories on p.EmployeeID equals empter.EmployeeID
                              join ter in ds.Territories on empter.TerritoryID equals ter.TerritoryID
                              select p;

    Add a TerritoryDescription property to the Employee class if it is missing and set all properties of the Employee class in your LINQ query:

    select new Employees { EmployeeID = p.EmployeeID, FirstName = p.FirstName, TerritoryDescription = ter.TerritoryDescription};

    The other option is to create a brand new class that contains only the EmployeeID, FirstName and TerritoryDescription properties and change the return type of your method (and the interface) to return a list of this type, e.g.:

    public List<YourNewClass> GetAllDataSetEmployees()
            {
                string connStr = @"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;";
    
                DataClasses1DataContext ds = new DataClasses1DataContext(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;");
    
                return (from p in ds.Employees
                             join empter in ds.EmployeeTerritories on p.EmployeeID equals empter.EmployeeID
                             join ter in ds.Territories on empter.TerritoryID equals ter.TerritoryID
                             select new YourNewClass { EmployeeID = p.EmployeeID, FirstName = p.FirstName, TerritoryDescription = ter.TerritoryDescription }).ToList;
            }
    

    But you won't be able to store anything other than Employees objects in a List<Employees>. This is a very basic rule of the language and object oriented programming in general.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by User3DX Wednesday, March 30, 2016 8:35 AM
    • Marked as answer by Md Zakir Saturday, April 2, 2016 6:24 AM
    Tuesday, March 29, 2016 6:21 PM
  • WCF forum is where you can post also.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf

    • Marked as answer by Md Zakir Saturday, April 2, 2016 6:25 AM
    Tuesday, March 29, 2016 9:34 PM

All replies

  • Your "result" query returns anonymous objects: https://msdn.microsoft.com/en-us/library/bb397696.aspx

    And you cannot store anonymous objects objects in a List<Employees>. You should select Employee objects:

    var result = from p in ds.Employees
                              join empter in ds.EmployeeTerritories on p.EmployeeID equals empter.EmployeeID
                              join ter in ds.Territories on empter.TerritoryID equals ter.TerritoryID
                              select p;

    Add a TerritoryDescription property to the Employee class if it is missing and set all properties of the Employee class in your LINQ query:

    select new Employees { EmployeeID = p.EmployeeID, FirstName = p.FirstName, TerritoryDescription = ter.TerritoryDescription};

    The other option is to create a brand new class that contains only the EmployeeID, FirstName and TerritoryDescription properties and change the return type of your method (and the interface) to return a list of this type, e.g.:

    public List<YourNewClass> GetAllDataSetEmployees()
            {
                string connStr = @"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True;";
    
                DataClasses1DataContext ds = new DataClasses1DataContext(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True;");
    
                return (from p in ds.Employees
                             join empter in ds.EmployeeTerritories on p.EmployeeID equals empter.EmployeeID
                             join ter in ds.Territories on empter.TerritoryID equals ter.TerritoryID
                             select new YourNewClass { EmployeeID = p.EmployeeID, FirstName = p.FirstName, TerritoryDescription = ter.TerritoryDescription }).ToList;
            }
    

    But you won't be able to store anything other than Employees objects in a List<Employees>. This is a very basic rule of the language and object oriented programming in general.

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by User3DX Wednesday, March 30, 2016 8:35 AM
    • Marked as answer by Md Zakir Saturday, April 2, 2016 6:24 AM
    Tuesday, March 29, 2016 6:21 PM
  • WCF forum is where you can post also.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=wcf

    • Marked as answer by Md Zakir Saturday, April 2, 2016 6:25 AM
    Tuesday, March 29, 2016 9:34 PM
  • Thanks it helped.

    But it is returning all the DataMembers of the class like lastName  is also returned as null.

    Saturday, April 2, 2016 7:06 AM