Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Return List From Joined Tables Using LINQ

Return List From Joined Tables Using LINQ

Scheduled Pinned Locked Moved C#
csharpdatabaselinq
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Liagapi
    wrote on last edited by
    #1

    I am trying to return a list from three tables which have been joined using LINQ. At the moment I can only get a list which contains data from one table instead of all three tables. Below is my code

    public List GetProductDetails(string productId)
    {
    var db = new ProductContext();
    IEnumerable emps = db.Employees.ToList();
    IEnumerable projs= db.Projects.ToList();
    IEnumerable prods = db.Products.ToList();

    var productionDetails = from e in emps
    join prj in projs on e.EmployeeID equals prj.ProjectID
    join prd in prods on e.EmployeeID equals prd.productID
    where prd.productID == Convert.ToInt32(productId)
    select new {employee = e.EmployeeName, project = prj.ProjectName , product = prd.ProductName };

    return productionDetails.Select(m=>m.EmployeeName).ToList();
    }

    I would like the list to contain employee, project, and product.

    G L Richard DeemingR 3 Replies Last reply
    0
    • L Liagapi

      I am trying to return a list from three tables which have been joined using LINQ. At the moment I can only get a list which contains data from one table instead of all three tables. Below is my code

      public List GetProductDetails(string productId)
      {
      var db = new ProductContext();
      IEnumerable emps = db.Employees.ToList();
      IEnumerable projs= db.Projects.ToList();
      IEnumerable prods = db.Products.ToList();

      var productionDetails = from e in emps
      join prj in projs on e.EmployeeID equals prj.ProjectID
      join prd in prods on e.EmployeeID equals prd.productID
      where prd.productID == Convert.ToInt32(productId)
      select new {employee = e.EmployeeName, project = prj.ProjectName , product = prd.ProductName };

      return productionDetails.Select(m=>m.EmployeeName).ToList();
      }

      I would like the list to contain employee, project, and product.

      G Offline
      G Offline
      Garth J Lancaster
      wrote on last edited by
      #2

      my first worries are :-

      public List<string> GetProductDetails(string productId)

      and

      return productionDetails.Select(m=>m.EmployeeName).ToList();

      why ? I would (because I understand it if I do it this way) a) define a 'prodDetails' POCO and use that instead of 'string' as the return list type, b) do a select 'new prodDetails {employee = e.EmployeeName .... instead of using an anonymous type c) remove the .Select in the return productionDetails clause

      L 1 Reply Last reply
      0
      • G Garth J Lancaster

        my first worries are :-

        public List<string> GetProductDetails(string productId)

        and

        return productionDetails.Select(m=>m.EmployeeName).ToList();

        why ? I would (because I understand it if I do it this way) a) define a 'prodDetails' POCO and use that instead of 'string' as the return list type, b) do a select 'new prodDetails {employee = e.EmployeeName .... instead of using an anonymous type c) remove the .Select in the return productionDetails clause

        L Offline
        L Offline
        Liagapi
        wrote on last edited by
        #3

        Hi, thank you so much for your response. Your solution would work great in most cases but what if the method is a RESTful WCF service method? Normally I would return a string or a list of string from a RESTful WCF Service or a RESTful Web API method so that the data can be sent across the web. If my method needs to return a complex type, how do I modify my method to make it work in a RESTful Service?

        G 1 Reply Last reply
        0
        • L Liagapi

          Hi, thank you so much for your response. Your solution would work great in most cases but what if the method is a RESTful WCF service method? Normally I would return a string or a list of string from a RESTful WCF Service or a RESTful Web API method so that the data can be sent across the web. If my method needs to return a complex type, how do I modify my method to make it work in a RESTful Service?

          G Offline
          G Offline
          Garth J Lancaster
          wrote on last edited by
          #4

          put a 'wrapper' around GetProductDetails maybe GetProductDetailsAsJSON and convert the returned list to a JSON encoded string

          using Newtonsoft.Json;

          and then ....

          string productId = string.Empty; // ** Init this not Empty
          List productionDetails = GetProductDetails(productId);
          string ProductDetailsJSON = Newtonsoft.Json.JsonConvert.SerializeObject(productionDetails);
          return ProductDetailsJSON;

          [edit] obviously on the other end when the REST package/data is received you have to do something like :- // assuming you have the REST Data in a string called 'json', pretty dumb, but its only an example List productionDetails = Newtonsoft.Json.JsonConvert.DeserializeObject>(json); [/edit]

          1 Reply Last reply
          0
          • L Liagapi

            I am trying to return a list from three tables which have been joined using LINQ. At the moment I can only get a list which contains data from one table instead of all three tables. Below is my code

            public List GetProductDetails(string productId)
            {
            var db = new ProductContext();
            IEnumerable emps = db.Employees.ToList();
            IEnumerable projs= db.Projects.ToList();
            IEnumerable prods = db.Products.ToList();

            var productionDetails = from e in emps
            join prj in projs on e.EmployeeID equals prj.ProjectID
            join prd in prods on e.EmployeeID equals prd.productID
            where prd.productID == Convert.ToInt32(productId)
            select new {employee = e.EmployeeName, project = prj.ProjectName , product = prd.ProductName };

            return productionDetails.Select(m=>m.EmployeeName).ToList();
            }

            I would like the list to contain employee, project, and product.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Liagapi wrote:

            At the moment I can only get a list which contains data from one table instead of all three tables.

            You are already creating one. See the line "select new {employee.." That would create a new anonymous type, and that would be harder to return :)

            Liagapi wrote:

            I would like the list to contain employee, project, and product.

            Create a new class that can hold the results, and return a list of that specific class.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

            1 Reply Last reply
            0
            • L Liagapi

              I am trying to return a list from three tables which have been joined using LINQ. At the moment I can only get a list which contains data from one table instead of all three tables. Below is my code

              public List GetProductDetails(string productId)
              {
              var db = new ProductContext();
              IEnumerable emps = db.Employees.ToList();
              IEnumerable projs= db.Projects.ToList();
              IEnumerable prods = db.Products.ToList();

              var productionDetails = from e in emps
              join prj in projs on e.EmployeeID equals prj.ProjectID
              join prd in prods on e.EmployeeID equals prd.productID
              where prd.productID == Convert.ToInt32(productId)
              select new {employee = e.EmployeeName, project = prj.ProjectName , product = prd.ProductName };

              return productionDetails.Select(m=>m.EmployeeName).ToList();
              }

              I would like the list to contain employee, project, and product.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Liagapi wrote:

              IEnumerable<Employee> emps = db.Employees.ToList(); IEnumerable<Project> projs= db.Projects.ToList(); IEnumerable<Product> prods = db.Products.ToList();

              Your code is loading the whole of those three tables into memory, before filtering for the specific product. That's going to be horribly inefficient.

              Liagapi wrote:

              string productId where prd.productID == Convert.ToInt32(productId)

              Your code is going to throw an exception if you pass in any string that can't be converted to an integer. Instead, you should change your parameter to be an integer, so that your expectations are obvious to the caller.

              Liagapi wrote:

              public List<string> GetProductDetails

              If you want to return something other than a single string for each record, you're going to need to change the return type.

              Liagapi wrote:

              join prj in projs on e.EmployeeID equals prj.ProjectID join prd in prods on e.EmployeeID equals prd.productID

              Those join columns don't look right to me. Double-check how your models are related.

              public sealed class ProductDetails
              {
              public string employee { get; set; }
              public string project { get; set; }
              public string product { get; set; }
              }
              ...
              public List<ProductDetails> GetProductDetails(int productId)
              {
              using (var db = new ProductContext())
              {
              var productDetails = from e in db.Employees
              join prj in db.Projects on e.EmployeeID equals prj.ProjectID // Is this the correct join?
              join prd in prods on e.EmployeeID equals prd.productID // Is this the correct join?
              where prd.productID == productId
              select new { e.EmployeeName, prj.ProjectName, prd.ProductName };

                  return productDetails.AsEnumerable().Select(x => new ProductDetails
                  {
                      employee = x.EmployeeName,
                      project = x.ProjectName,
                      product = x.ProductName
                  }).ToList();
              }
              

              }

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              L 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Liagapi wrote:

                IEnumerable<Employee> emps = db.Employees.ToList(); IEnumerable<Project> projs= db.Projects.ToList(); IEnumerable<Product> prods = db.Products.ToList();

                Your code is loading the whole of those three tables into memory, before filtering for the specific product. That's going to be horribly inefficient.

                Liagapi wrote:

                string productId where prd.productID == Convert.ToInt32(productId)

                Your code is going to throw an exception if you pass in any string that can't be converted to an integer. Instead, you should change your parameter to be an integer, so that your expectations are obvious to the caller.

                Liagapi wrote:

                public List<string> GetProductDetails

                If you want to return something other than a single string for each record, you're going to need to change the return type.

                Liagapi wrote:

                join prj in projs on e.EmployeeID equals prj.ProjectID join prd in prods on e.EmployeeID equals prd.productID

                Those join columns don't look right to me. Double-check how your models are related.

                public sealed class ProductDetails
                {
                public string employee { get; set; }
                public string project { get; set; }
                public string product { get; set; }
                }
                ...
                public List<ProductDetails> GetProductDetails(int productId)
                {
                using (var db = new ProductContext())
                {
                var productDetails = from e in db.Employees
                join prj in db.Projects on e.EmployeeID equals prj.ProjectID // Is this the correct join?
                join prd in prods on e.EmployeeID equals prd.productID // Is this the correct join?
                where prd.productID == productId
                select new { e.EmployeeName, prj.ProjectName, prd.ProductName };

                    return productDetails.AsEnumerable().Select(x => new ProductDetails
                    {
                        employee = x.EmployeeName,
                        project = x.ProjectName,
                        product = x.ProductName
                    }).ToList();
                }
                

                }

                L Offline
                L Offline
                Liagapi
                wrote on last edited by
                #7

                Thank you all for replying. The joins are correct and I was able to get the results I was looking for. I've learned a lot from this exercise and your responses to my post helped tremendously.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups