简体   繁体   English

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

[英]Replicating a SQL WITH statement query in Linq to Entities

I'm looking to recreate a query that works for me when I wrote it in SQL but I cannot find out how to recreate in LINQ. 我想重新创建一个对我有用的查询,当我用SQL编写该查询时,却找不到如何在LINQ中重新创建的查询。 I am trying to get the latest logon record for each user on the system. 我正在尝试获取系统上每个用户的最新登录记录。 It is part of a search function so there will be WHEREs added onto the query. 它是搜索功能的一部分,因此将在查询中添加WHERE。 This SQL does what I want it to do 这个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;

Each person can have more than 1 licence and I'm trying to return the last licence that was used. 每个人可以拥有1个以上的许可证,我正在尝试退还使用的最后一个许可证。 Is there a way to create this query using LINQ to Entities? 有没有一种方法可以使用LINQ to Entities创建此查询? Is there a better way to write the query? 有没有更好的方法来编写查询?

I think you can do something like this: 我认为您可以执行以下操作:

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();

Below is a consoleApp I wrote for your question: 以下是我为您的问题编写的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; }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM