locked
Get data from another table using LINQ Lambda RRS feed

  • Question

  • User1242168447 posted

    I have below Lambda-LINQ line that works fine, the only issue is it's displaying the BusinessType name for the company dbcontext, whereas it should display Username associated with the current student dbcontext

    They are two separate tables with no navigation property.

    I need to be able to retrieve the Username from Student table from that Lambda-LINQ line exactly in x.BusinessType. So instead of x.BusinessType from Company table, it should be Username from Student table, I mean it should be the **Student.Username (according to the CompanyId that was saved along with)**

    I tried join method but no success.

    // This table is saved with StudentId from table Student in another part of the app and there is no navigation property as there are both separate tables.

    public class Company
    {
        public int Id { get; set; }
    
        public string Description { get; set; }
        
        public string BusinessType { get; set; }
    
        public int ParentId { get; set; }
        
        public string Description { get; set; }
        
        public int StudentId { get; set; }        // Saves Id from student table
    }
    
        public class Student
        {
            public int Id { get; set; }
            
            public string Email { get; set; }
    
            public string Username { get; set; }
    
            public bool IsActive { get; set; }
    
            public string Name { get; set; }
    
            public string Image { get; set; }
        }
        
    
        var company = _context.Company.ToList();
        var student = _context.Student.ToList();
        
        
        tree.AddRange(company.Select(x => new TreeObject { id = x.Id.ToString(), text = HttpUtility.HtmlEncode(x.BusinessType), parent = x.ParentId.ToString(), state = new { opened = false }, type = "person", data = new { level = "person", name = x.BusinessType, description = x.Description, datahash = HashItemData(new company { Id = x.Id, Description = x.Description }) } }));
        
    I tried below line but no luck tree.AddRange(company.Select(x => new TreeObject { id = x.Id.ToString(), text = HttpUtility.HtmlEncode(x.BusinessType).Join(users, person => person, user => user.Id, (person, users) => person), parent = x.ParentId.ToString(), state = new { opened = false }, type = "person", data = new { level = "person", name = x.BusinessType, description = x.Description, datahash = HashItemData(new company { Id = x.Id, Description = x.Description }) } }));

    Wednesday, October 7, 2020 6:32 PM

Answers

  • User-2054057000 posted

    You should use Navigation Property and avoid JOIN. See an Entity Framework Core project. Using a Join makes queries long and confusing. In your case you should break the query and try debugging the code like:

    var myQuery = "your linq query";
    tree.AddRange(myQuery);

    Put Visual Studio breakpoint on the first line of code and check what value is coming to the "myQuery" variable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 5:09 AM
  • User1312693872 posted

    Hi,LetMeCode

    LetMeCode

    I need to be able to retrieve the Username from Student table from that Lambda-LINQ line exactly in x.BusinessType.

    First I want to ask you whether the StudentId is the value associated with company and student? If so, you can refer to my linq which uses the join method:

    var tables = (from stu in _context.Students
                              join com in _context.Companys
                              on stu.Id equals com.StudentId
                              select new
                              {
                                  name = stu.Username,
                                  type = com.BusinessType
                              }).ToList();

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 8:28 AM

All replies

  • User-2054057000 posted

    You should use Navigation Property and avoid JOIN. See an Entity Framework Core project. Using a Join makes queries long and confusing. In your case you should break the query and try debugging the code like:

    var myQuery = "your linq query";
    tree.AddRange(myQuery);

    Put Visual Studio breakpoint on the first line of code and check what value is coming to the "myQuery" variable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 5:09 AM
  • User1312693872 posted

    Hi,LetMeCode

    LetMeCode

    I need to be able to retrieve the Username from Student table from that Lambda-LINQ line exactly in x.BusinessType.

    First I want to ask you whether the StudentId is the value associated with company and student? If so, you can refer to my linq which uses the join method:

    var tables = (from stu in _context.Students
                              join com in _context.Companys
                              on stu.Id equals com.StudentId
                              select new
                              {
                                  name = stu.Username,
                                  type = com.BusinessType
                              }).ToList();

    Result:

    Best Regards,

    Jerry Cai

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 8, 2020 8:28 AM
  • User1064314092 posted

    2 Answers

    You Can Try.

    https://stackoverflow.com/questions/25370019/linq-select-item-where-it-is-equal-to-id-in-another-table

    Thursday, October 8, 2020 9:25 AM