0

I created 2 tables and 2 model classes, one for Lookups and another one for Lookup_Types.

All CRUD operations and indexes for Lookup_Types are OK, but then I need to return Lookup_name not Lookup_type_id.

My models :

 public class Lookup_Type
 {
        [Key]
        public int Id { get; set; }

        [Required(ErrorMessage = "This field is required")]
        [StringLength(50, MinimumLength = 3,
         ErrorMessage = "Name Should be minimum 3 characters and a maximum of 50 characters")]
        public string Lookup_name { get; set; }
 }

public class Lookup
{
    [Key]
    public int Id { get; set; }

    [Required(ErrorMessage = "This field is required")]
    [StringLength(50, MinimumLength = 3,
        ErrorMessage = "Name Should be minimum 3 characters and a maximum of 50 characters")]
    public string Lookup_name { get; set; }

    public int Lookup_type_id { get; set; }
}

public async Task<IActionResult> GetAll()
{
   return Json(new { data = await _db.Lookups.ToListAsync() });
}

In GetAll(), I need to return Lookup_name in Lookup_Type model, not Lookup_type_id

1
  • Assuming that _db.Lookups.ToListAsync() returns a list of Lookup instances, do you want to map that list to a list of strings with the value of Lookup_name? Is Lookup_name a column with a valid value that is being mapped from the underlying table? Commented May 1, 2020 at 3:03

2 Answers 2

0

Here is a working demo:

public async Task<IActionResult> GetAll()
{
    var data =  from l in _context.Lookup
                join t in _context.Lookup_Type
                on l.Lookup_type_id equals t.Id

                select new
                {
                    l.Id,
                    l.Lookup_name,
                    Lookup_Type_name = t.Lookup_name
                };
    return Json(new { data = await data.ToListAsync() });         
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can join two table, but in Lookup_Type table, name should change to Lookup_Type_name

 data = await from x in _db.Lookups
                             join y in db.Lookup_Type
                             on x.Lookup_type_id equals y.Id

                             select new
                             {
                                 Id = x.Id,
                                 Lookup_name = x.Lookup_name ,
                                 Lookup_Type_name = y.Lookup_Type_name

                             }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.