Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i want to bind the only image in "a.BlogPicturePath" which is stored at its maximum Id value.

How do i use join query to get the max id value of second table?

What I have tried:

{
    try
    {
        using (database db = new database())
        {



            var query = (
                          from c in db.tblBlogs

                         join a in db.tblBlogMedias on c.id equals  a.BlogId



                         select new
                        {

                            c.id,
                            c.CreatedDate,
                            c.Author,
                            c.BlogTitle,
                            c.BlogDescription,
                            a.BlogId,
                            a.BlogPicturePath
                        }).OrderByDescending(d => d.id).ToList();



            System.Text.StringBuilder sb = new System.Text.StringBuilder();



                 query.ToList().ForEach(x =>
                    {



                            sb.Append(string.Format("<div class='post-thumb'><a href='#'><img class='img-responsive' src='{0}' alt=''></a><div class='post-meta'>"+
                                                     "</div>"+
                                                    "</div>"+
                                                    "<div class='entry-header'><h3><a href='#'>{1}</a></h3><span class='date'>{2}</span></div>",x.BlogPicturePath,x.BlogTitle,x.CreatedDate));


                    });





                 blogdiv.InnerHtml = sb.ToString();

                 Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "activateReadmore()", true);


        }




    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }

}
Posted
Updated 3-Nov-17 12:40pm
Comments
Umair Nafis 1-Nov-17 13:31pm    
how to write this query in entity framework . ie the code written above:

select tblBlog.*,tblBlogMedia.BlogPicturePath from tblBlog left outer join tblBlogMedia on tblBlog.id = tblBlogMedia.BlogId
where tblBlogMedia.id=(select max(id) from tblBlogMedia where BlogId='1')

here BlogId='1' is just for example i want samething like BlogId = @blogid
Karthik_Mahalingam 1-Nov-17 23:41pm    
try something like this
result.Where(k=>k.id = result.Max(a=>a.id).where(b=>b.blogid = 1) )
Umair Nafis 3-Nov-17 15:06pm    
How>?? what is result in this.? and where to use it ?
Karthik_Mahalingam 3-Nov-17 15:12pm    
query

1 solution

var q = (from d in db.tblBlogMedias
                             join c in db.tblBlogs on d.BlogId equals x.id
                             select new
                             {
                                 d.Id,
                                 d.BlogPicturePath
                             }).OrderByDescending(d=>d.Id).Max(d => d.BlogPicturePath);



I write this query in foreach loop and it works pefectly fine. i just pass the value of q in sb.append
 
Share this answer
 
Comments
Richard Deeming 6-Nov-17 13:57pm    
Calling OrderByDescending followed by Max doesn't make any sense. If you want the maximum value of a column, it doesn't matter what order the data is in.

If you just want the picture for the item with the highest ID, use:
.OrderByDescending(d => d.Id).Select(d => d.BlogPicturePath).FirstOrDefault()


Also, you don't need the join in that query, since you're not using anything from the tblBlogs table.
Umair Nafis 6-Nov-17 15:24pm    
ohh yeah ! actually you are absolutely right and i didnt recognise it before. lol
Thank You Sir.

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