如何将左外连接sql查询转换为实体框架5.0?

时间:2017-11-01 19:53:19

标签: c# mysql asp.net entity-framework

   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='col-sm-4 
wow fadeInUp animated' data-wow-duration='1000ms' data-wow-delay='400ms' 
style='visibility: visible; animation-duration: 1000ms; animation-delay: 
400ms; animation-name: fadeInUp;'><div class='post-thumb'><div class='post-
meta'>"+
                                                     "</div>"+
                                                    "</div>"+
                                                    "<div class='entry-
header'><h3><a href='#'>{0}</a></h3><span class='date'>{1}</span></div>
</div>",x.BlogTitle,x.CreatedDate));

                    });
                 }

如何在var query =:

中编写此sql查询
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='2')

2 个答案:

答案 0 :(得分:0)

使用正确命名的实体和导航属性,它看起来像这样:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EfDbFirstTest
{
    public class Blog
    {
        public int Id { get; set; }
        public DateTime CreatedDate { get; set; }
        public string Author { get; set; }
        public string BlogTitle { get; set; }
        public string BlogDescription { get; set; }
        public ICollection<BlogMedia> BlogMedia { get; set; }
    }

    public class BlogMedia
    {
        public int Id { get; set; }
        public int BlogId { get; set; }
        public Blog Blog { get; set; }
        public string BlogPicturePath { get; set; }
    }

    public class Db : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<BlogMedia> BlogMedia { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {


            using (var db = new Db())
            {
                db.Database.Log = m => Console.WriteLine(m);

                var q = from b in db.Blogs
                        select new
                        {

                            b.Id,
                            b.CreatedDate,
                            b.Author,
                            b.BlogTitle,
                            b.BlogDescription,
                            BlogPicturePath = b.BlogMedia.Any() ? b.BlogMedia.OrderByDescending(m => m.Id).FirstOrDefault().BlogPicturePath : null
                        };

                var results = q.ToList();



                Console.ReadKey();
            }
        }
    }
}

答案 1 :(得分:0)

这就是我想要的完美工作方式,我在ForEach循环体中编写这段代码,然后在sb.append中传递q的值:

function launchTutorial() {
        HideFloatingMenu();  //freezes on page and it doesn't when i comment out the subsequent array loop
    //highlightElement("diLeftColumn");

//the classes of each element to highlight in the tutorial

    var tutorialClasses = [ 
        "diLeftColumn",
        "diMiddleColumn",
        "diRightColumn"
    ];

    var threeSec = new Date().getTime() + 3000;
    for (var i = 0; i < tutorialClasses.length; i++) {
        //$.each(tutorialClasses, function (key, value) {

        if (i != 0) {
            var now = new Date().getTime();
            if (now >= threeSec) {
                highlightElement(tutorialClasses[i]);
                threeSec = new Date().getTime() + 3000;
            }
            else {
                i = i - 1; //go back to this item if it hasn't been 3 seconds
            }
        }
        else {
            highlightElement(tutorialClasses[i]);
            threeSec = new Date().getTime() + 3000;
        }
  }
}