简体   繁体   English

实体框架按日期获得每种组合之一

[英]Entity Framework get one of each combination by date

I'm using Entity Framework in ASP.NET. 我在ASP.NET中使用实体框架。 I have something like a table like this: 我有类似这样的表:

+----+--------------------------+-------+-------+------------+
| id |         content          | type1 | type2 |    date    |
+----+--------------------------+-------+-------+------------+
|  0 | Some text                | TypeA | TypeB | 2013-04-01 |
|  1 | Some older text          | TypeB | TypeA | 2012-03-01 |
|  2 | Some even older texttext | TypeB | TypeC | 2011-01-01 |
|  3 | A dog                    | TypeC | TypeB | 2013-04-01 |
|  4 | And older dog            | TypeC | TypeB | 2012-03-01 |
|  5 | An even older dog        | TypeA | TypeC | 2011-01-01 |
|  6 | More text                | TypeA | TypeB | 2013-03-01 |
+----+--------------------------+-------+-------+------------+

I already can obtain the most recent occurrence of type 1 or type 2, but I want to query the database to obtain the most recent occurrence of the combination of two types: 我已经可以获取类型1或类型2的最新出现,但是我想查询数据库以获得两种类型的组合的最新出现:

 +----+--------------------------+-------+-------+------------+ | id | content | type1 | type2 | date | +----+--------------------------+-------+-------+------------+ | 0 | Some text | TypeA | TypeB | 2013-04-01 | | 3 | A dog | TypeC | TypeB | 2013-04-01 | | 5 | An even older dog | TypeA | TypeC | 2011-01-01 | +----+--------------------------+-------+-------+------------+ 

Thanks! 谢谢!

EDIT: The columns type1 or type2 are basically the same, so if Type1=A and Type2=B it's the same as Type1=B and Type2=A. 编辑:列type1或type2基本相同,因此,如果Type1 = A和Type2 = B,则与Type1 = B和Type2 = A相同。

This would do the trick, except for one little issue, which I'll describe after the code sample. 除了一个小问题,这将达到目的,我将在代码示例之后对其进行描述。

var grouped = datacontext.Table
    .GroupBy(
        item => new { item.Type1, item.Type2 },
        (key, groupData) => groupData.OrderBy(x => x.TheDate).First())
    .ToArray();

The problem is that this way the grouping is done on the combination of Type1 and Type2, in that order . 问题是,这样的分组上Type1和Type2的组合完成, 在这个顺序 So what you are saying in the comments, which is that Type1=A AND Type2=B is equal to Type1=B AND Type2=A is a problem. 因此,您在注释中说的是Type1=A AND Type2=B等于Type1=B AND Type2=A

I really have no idea if EF (or L2S) can compile this, but it is worth a try. 我真的不知道EF(或L2S)是否可以编译此文件,但值得尝试。

var grouped = datacontext.Table
    .Select(x => new Data // TODO: Update to the correct typename
    {
        x.Id,
        x.Content,
        Type1 = x.Type1.CompareTo(x.Type2) > 0 ? x.Type2 : x.Type1,
        Type2 = x.Type1.CompareTo(x.Type2) > 0 ? x.Type1 : x.Type2,
        x.TheDate
    })
    .GroupBy(
        item => new { item.Type1, item.Type2 },
        (key, groupData) => groupData.OrderBy(x => x.TheDate).First())
    .ToArray();

If the above doesn't compile, then an alternative option is to correct the Type1/Type2 values after the database-grouping, and perform the grouping again in-memory. 如果以上内容未编译,则另一种选择是在数据库分组后更正Type1 / Type2值,然后在内存中再次执行分组。 Yes, this means it will group twice (once by the database, once in-memory), but it will work, and won't require to import the complete table into memory. 是的,这意味着它将进行两次分组(一次由数据库执行,一次在内存中执行),但是它将起作用,并且不需要将整个表导入内存。

var grouped = datacontext.Table
    .GroupBy(
        item => new { item.Type1, item.Type2 },
        (key, groupData) => groupData.OrderBy(x => x.TheDate).First())
    .AsEnumerable()
    .GroupBy(
        item => new {
            // Group by the possible-swapped-values of Type1 and Type2
            Type1 = x.Type1.CompareTo(x.Type2) > 0 ? x.Type2 : x.Type1
            Type2 = x.Type1.CompareTo(x.Type2) > 0 ? x.Type1 : x.Type2
        },
        (key, groupData) => groupData.OrderBy(x => x.TheDate).First())
    .ToArray();

I think I like the last option the best, since: 我认为我最喜欢最后一个选项,因为:

  • it can be translated to sql (no special features like string.CompareTo ) 可以将其转换为sql(没有特殊功能,例如string.CompareTo
  • at most it will be twice the amount of records that the database will yield, not anymore (which in my opinion is acceptable without knowing the amount of total data we're talking about) 最多将是数据库产生的记录数量的两倍,不再是(我认为在不知道我们正在谈论的总数据量的情况下可以接受)

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

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