Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 3 tables 
1. Tbl_Model,
2. Tbl_ModelImg,
3.Tbl_Category

what i want is to fetch the records from these 3 tables on behalf of categoryId.

The single model may have multiple images, but i want to show all products of that category with their images. The problem is i want only single image in that view. so that when a user click on that particular model, the details of that model and all its images will show on next view.


The following query is working fine but it displays all the images with their model name. Means If a model have 4 images than on categorydetails page it display 4 items with the same name and different images.

here is the model class :


What I have tried:

public class showdata
{
    public Tbl_ModelImg tmi { get; set; }
    public Tbl_Model tm { get; set; }
    public Tbl_SubCategory tblsubcategory { get; set; }
}



 public ActionResult Categorydetails(string sid)
    {
     var sId = Int64.Parse(new
               StandardModule().Decrypt(HttpUtility.UrlDecode(sid.ToString())));
     try
       {
          var query = (from c in db.Tbl_Model
         join o in db.Tbl_ModelImg
          on c.Model_Id equals o.Model_Id
         join d in db.Tbl_SubCategory on c.SubCategory_Id equals d.Id
         where c.SubCategory_Id == sId
         select new showdata()
         {
                    tm = c,
            tmi = o,
            tblsubcategory = d
         }).OrderByDescending(d => d.tm.Id).ToList();

    var squery = (from c in db.Tbl_SubCategory where c.Id == sId select
                          c).FirstOrDefault();
    ViewBag.SubcategoryName = squery.SubCategory_Name;

    return View(query);
          }
          catch(exception e)
             {
             Response.Write(e.Message);
               }
}
Posted
Updated 27-Mar-19 20:13pm

If you just want a single row to be returned then you can simply use the LINQ .Single() or .First() extension method:

C#
var query = (from c in db.Tbl_Model
         join o in db.Tbl_ModelImg
          on c.Model_Id equals o.Model_Id
         join d in db.Tbl_SubCategory on c.SubCategory_Id equals d.Id
         where c.SubCategory_Id == sId
         select new showdata()
         {
                    tm = c,
            tmi = o,
            tblsubcategory = d
         }).OrderByDescending(d => d.tm.Id);

//get all result

var details = query.ToList();

//get single result

var commonImage = query.First();


Note that the .ToList() method will return all the rows based from your query.
 
Share this answer
 
v2
Comments
Umair Nafis 27-Mar-19 13:06pm    
Thanks for the reply sir,
I used ToList() because i want all the rows from tbl_Model but a single row from tbl_ModelImg.
From above query which i wrote im getting all the rows from both tables, so i just want to write a query which return all the rows from tbl_Model and single row from tbl_ModelImg
Vincent Maverick Durano 27-Mar-19 14:15pm    
You may need two separate queries to handle that. One for getting all the details and one query to get a single row from your tblModelImg.
Umair Nafis 27-Mar-19 18:46pm    
means i can not join the query like i want >??
Vincent Maverick Durano 27-Mar-19 22:03pm    
You can of course. I mean you need two separate result set to do that. I've updated the solution for your reference.
Umair Nafis 28-Mar-19 2:15am    
Thank you for support sir , here i posted the final query through which i got the actual result. U can see there .
var query = (from c in db.Tbl_Model join o in db.Tbl_ModelImg.GroupBy(m =>
                             m.Model_Id).Select(m=> m.FirstOrDefault()) on c.Model_Id equals o.Model_Id
                            join d in db.Tbl_SubCategory on c.SubCategory_Id equals d.Id
                            where c.SubCategory_Id == sId
                            select new showdata()
                             {
                               tm = c,
                               tmi = o,
                               tblsubcategory = d


                              }).OrderByDescending(d => d.tm.Id).ToList();
 
Share this answer
 
Comments
Vincent Maverick Durano 28-Mar-19 11:09am    
Great. Glad you sorted it out. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900