Binding Model to LINQ query using DBcontext in C#

Mar 11 2022 12:19 PM

Hi,

I had following Model:
 

public class UserInterestModel
    {
        public List<category> category { get; set; }
        public string Type { get; set; }
    }

    public class category

    {
        public int categoryid { get; set; }
        public List<subcategory> subcategories { get; set; }
    }
    public class subcategory
    {
        public int subCategoryId { get; set; }
        public string? subCategoryName { get; set; }
       
    }

Database tables:

usercategory : id, categoryname, subcategoryid, isActive

usersubcategory : id, subcategoryname, isActive

I want to retrieve the data from these two tables. For that I am writing the following code 
 

UserInterestModel userInterestModel = new UserInterestModel();
List<category> categories = null;
List<subcategory> subCategories = null;

categories = (from c in this._dataContext.Usercategories
                          join sc in this._dataContext.Usersubcategories on
                          c.Subcategoryid equals sc.Id
                          where c.Isactive == true && sc.Isactive == true
                          select new subCategories

 

Can  you help me to complete the code or suggest the solution how i can read the data and bind it to my UserInterestModel


Answers (1)