Linq-to-sql一对多,最多

时间:2022-08-24 20:50:10

I'm having a heck of a time with this one. Might be the lack of sleep... or maybe I'm just getting dumb.

我对这个问题感到兴奋。可能是睡眠不足......或者我可能只是变得愚蠢。

I have 2 tables:
a) Person {Key, Name, LastName}
b) LogEntry {Key, PersonKey, Log, EntryTime}

我有2个表:a)人{Key,Name,LastName} b)LogEntry {Key,PersonKey,Log,EntryTime}

I'm trying to get a join of Person and LogEntry where LogEntry is the latest LogEntry for the given Person.

我正在尝试获取Person和LogEntry的连接,其中LogEntry是给定Person的最新LogEntry。

I hope I don't go "duh..." in five minutes when the answers hits me hard.

我希望在五分钟内我没有“呃...”,当答案让我痛苦的时候。

3 个解决方案

#1


If you have the Relations in SQLServer you should have in the class Person something like Person.LogEntries. You can do like this:

如果你在SQLServer中有关系,你应该在类Person中有Person.LogEntries。你可以这样做:

   LogEntry logEntry = 
Person.LogEntries.OrderByDescending(p => p.EntryTime).FirstOrDefault();

Then you can check if logEntry is null if not I should have the last log entry for the person.

然后你可以检查logEntry是否为null如果不是我应该有该人的最后一个日志条目。

#2


If one does not have relationships setup, the accepted answer can be done by:

如果没有设置关系,则可以通过以下方式完成接受的答案:

var r = Person
    .GroupJoin(
        LogEntry,
        o => o.Key,
        i => i.PersonKey,
        (o,i) => new {o, Entry = i.OrderByDescending(x => x.EntryTime).First()});

#3


On a partial class for Person, create a new property similar to this:

在Person的部分类上,创建一个与此类似的新属性:

public partial Person
{
    public LogEntry RecentLogEntry
    {
        get
        {
            return LogEntries.OrderByDescending(l => l.EntryTime).FirstOrDefault();
        }
    }
}

I believe this (or something similar) should work, though this is from memory. This, of course, relies on your relationships being present.

我相信这(或类似的东西)应该有效,尽管这是来自记忆。当然,这取决于您的关系存在。

If you would prefer to do this when selecting your records, you could try this:

如果您在选择记录时更愿意这样做,可以尝试这样做:

var people = from p in context.People
             select new
             {
                 Person = p,
                 LatestLogEntry = p.LogEntries.OrderByDescending(l => l.EntryTime).FirstOrDefault()
             };

#1


If you have the Relations in SQLServer you should have in the class Person something like Person.LogEntries. You can do like this:

如果你在SQLServer中有关系,你应该在类Person中有Person.LogEntries。你可以这样做:

   LogEntry logEntry = 
Person.LogEntries.OrderByDescending(p => p.EntryTime).FirstOrDefault();

Then you can check if logEntry is null if not I should have the last log entry for the person.

然后你可以检查logEntry是否为null如果不是我应该有该人的最后一个日志条目。

#2


If one does not have relationships setup, the accepted answer can be done by:

如果没有设置关系,则可以通过以下方式完成接受的答案:

var r = Person
    .GroupJoin(
        LogEntry,
        o => o.Key,
        i => i.PersonKey,
        (o,i) => new {o, Entry = i.OrderByDescending(x => x.EntryTime).First()});

#3


On a partial class for Person, create a new property similar to this:

在Person的部分类上,创建一个与此类似的新属性:

public partial Person
{
    public LogEntry RecentLogEntry
    {
        get
        {
            return LogEntries.OrderByDescending(l => l.EntryTime).FirstOrDefault();
        }
    }
}

I believe this (or something similar) should work, though this is from memory. This, of course, relies on your relationships being present.

我相信这(或类似的东西)应该有效,尽管这是来自记忆。当然,这取决于您的关系存在。

If you would prefer to do this when selecting your records, you could try this:

如果您在选择记录时更愿意这样做,可以尝试这样做:

var people = from p in context.People
             select new
             {
                 Person = p,
                 LatestLogEntry = p.LogEntries.OrderByDescending(l => l.EntryTime).FirstOrDefault()
             };