Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 连接2个表和跳过记录时获取不适当的记录_C#_Entity Framework_Linq - Fatal编程技术网

C# 连接2个表和跳过记录时获取不适当的记录

C# 连接2个表和跳过记录时获取不适当的记录,c#,entity-framework,linq,C#,Entity Framework,Linq,这是我的表的两个属性: public class Test { public int Id { get; set; } public string Version { get; set; } public virtual ICollection<TestOperation> TestOperation { get; set; } } public class TestOperation { public int Id { get; set; }

这是我的表的两个属性:

public class Test
{
    public int Id { get; set; }
    public string Version { get; set; }
    public virtual ICollection<TestOperation> TestOperation { get; set; }
}


public class TestOperation
{
    public int Id { get; set; }
    public Nullable<int> TestId { get; set; }
    public virtual Test Test { get; set; }
}
以下查询的测试操作表输出:

TestId   Version
339        1
340        2
341        3
342        4
343        5
344        6
345        7
346        8
347        9
348        10
349        11
350        12
351        13
352        14
select TestId, count(*) from [dbo].[TestOperation] group by TestId

TestId    Count(*)
340        10
341        21
342        41
343        8
344        30
345        21
346        18
347        5
348        20
349        31
350        34
351        20
352        16
TestId  Version
340       2
341       3
342       4
343       5
344       6
345       7
346       8
347       9
348       10
349       11
350       12
var output = (from m in context.Test
              join c in context.TestOperation on m.Id equals c.TestId
              orderby m.Id descending
              select new
              {
                  testId = m.Id,
                  version = m.Version
              }).Skip(2).Distinct().ToList();
预期输出跳过以上输出的最后2条记录:

TestId   Version
339        1
340        2
341        3
342        4
343        5
344        6
345        7
346        8
347        9
348        10
349        11
350        12
351        13
352        14
select TestId, count(*) from [dbo].[TestOperation] group by TestId

TestId    Count(*)
340        10
341        21
342        41
343        8
344        30
345        21
346        18
347        5
348        20
349        31
350        34
351        20
352        16
TestId  Version
340       2
341       3
342       4
343       5
344       6
345       7
346       8
347       9
348       10
349       11
350       12
var output = (from m in context.Test
              join c in context.TestOperation on m.Id equals c.TestId
              orderby m.Id descending
              select new
              {
                  testId = m.Id,
                  version = m.Version
              }).Skip(2).Distinct().ToList();
请注意,在
测试操作
表中,我没有测试Id 339的任何记录

查询:

TestId   Version
339        1
340        2
341        3
342        4
343        5
344        6
345        7
346        8
347        9
348        10
349        11
350        12
351        13
352        14
select TestId, count(*) from [dbo].[TestOperation] group by TestId

TestId    Count(*)
340        10
341        21
342        41
343        8
344        30
345        21
346        18
347        5
348        20
349        31
350        34
351        20
352        16
TestId  Version
340       2
341       3
342       4
343       5
344       6
345       7
346       8
347       9
348       10
349       11
350       12
var output = (from m in context.Test
              join c in context.TestOperation on m.Id equals c.TestId
              orderby m.Id descending
              select new
              {
                  testId = m.Id,
                  version = m.Version
              }).Skip(2).Distinct().ToList();

上面的查询给了我
test351
,这是错误的,因为我不想要测试表中的最后2条记录,在我的查询中,我从列表中跳过了2条记录。那么为什么我会看到测试ID 351呢?

请注意设置代码和文本的格式。接下来,我们还不清楚为什么您希望不会看到测试351的任何记录。。。例如,如果您有两条测试352的记录,您的
Skip(2)
将跳过这两个合并的结果。您的样本数据没有重复值,但如果您的真实数据集有重复值,则您可能应该在
Skip
之前执行
Distinct
,而不是之后执行。这就是问题所在吗?对不起,这还不清楚。您正在按ID降序排序,并跳过前两个值。。。因此,如果有两个条目对应于ID352,那么您可以跳过这些条目,并仍然得到351的记录。如果您能给我们提供完整的样本数据以及预期和实际的结果,这将非常有帮助。(如果我是你,我也会在LINQ to Objects中尝试同样的事情……这意味着你可以发布a。)对,所以也将排序移到
Distinct()
之后。这对我来说当然是合理的。