在Linq中复制SQL WITH语句查询到实体

时间:2014-04-02 15:32:19

标签: sql vb.net linq entity-framework linq-to-entities

我想在SQL中编写它时重新创建一个对我有用的查询,但我无法找到如何在LINQ中重新创建。我试图获取系统上每个用户的最新登录记录。它是搜索功能的一部分,因此会在查询中添加WHERE。这个SQL做了我想要它做的事情

with latest_logon_for_each_user as (select user_id as userId, max(logon_date) as maxLatestLogon
    from licence
    group by fk_user_id)
    select * 
    from licence ul join users u on ul.fk_user_id = u.id,
    latest_logon_for_each_user 
    where ul.fk_user_id = userId and ul.date_of_latest_logon = maxLatestLogon
    order by ul.id;

每个人可以拥有1个以上的许可证,并且我试图返回上次使用的许可证。有没有办法使用LINQ to Entities创建此查询?有没有更好的方法来编写查询?

1 个答案:

答案 0 :(得分:0)

我认为你可以这样做:

var result = (from u in users
                                from ul in licences
                                where u.id == ul.user_id
                                && ul.logon_date == ((from d in licences where u.id == d.user_id select d.logon_date).Max())
                                select new QueryItem()
                                {
                                    id = u.id,
                                    logon_date = ul.logon_date
                                }).Distinct().ToList();

下面是我为您的问题编写的consoleApp:

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

namespace StackOverFlowConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            List<users> users = new List<users>()
            {
                new users(){id =1},
                new users(){id =2},
                new users(){id =3}

            };

            List<licence> licences  = new List<licence>()
            {
                new licence(){user_id=1, logon_date=DateTime.Today},
                new licence(){user_id=1, logon_date=DateTime.Today.AddDays(-3)},
                new licence(){user_id=1, logon_date=DateTime.Today.AddDays(-1)},
                new licence(){user_id=1, logon_date=DateTime.Today.AddDays(-2)},
                new licence(){user_id=2, logon_date=DateTime.Today},
                new licence(){user_id=3, logon_date=DateTime.Today}
            };

            var result = (from u in users
                                from ul in licences
                                where u.id == ul.user_id
                                && ul.logon_date == ((from d in licences where u.id == d.user_id select d.logon_date).Max())
                                select new QueryItem()
                                {
                                    id = u.id,
                                    logon_date = ul.logon_date
                                }).Distinct().ToList();


        }

        class licence
        {
            public int user_id { get; set; }            
            public DateTime logon_date { get; set; }

        }

        class users 
        {
            public int id { get; set; }

        }

        class QueryItem
        {

            public int id { get; set; }

            public DateTime logon_date { get; set; }
        }

    }
}