Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.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# 基于Linq渐进状态的查询_C#_.net_Linq_Entity Framework_Linq To Entities - Fatal编程技术网

C# 基于Linq渐进状态的查询

C# 基于Linq渐进状态的查询,c#,.net,linq,entity-framework,linq-to-entities,C#,.net,Linq,Entity Framework,Linq To Entities,首先,我为这个糟糕的标题道歉。完全不知道该如何描述这个问题 我有一个关系实体,它定义了两个用户之间的关系 public class Relationship{ User User1{get;set;} User User2{get;set;} DateTime StateChangeDate {get;set;} //RelationshipState is an Enum with int values State RelationshipState State{get;set;}

首先,我为这个糟糕的标题道歉。完全不知道该如何描述这个问题

我有一个关系实体,它定义了两个用户之间的关系

public class Relationship{
 User User1{get;set;}
 User User2{get;set;}
 DateTime StateChangeDate {get;set;}

 //RelationshipState is an Enum with int values
 State RelationshipState State{get;set;}
}
关系状态示例

public enum RelationshipState{
 state1 = 1,
 state2 = 2, 
 state3 = 3,
 state4 = 4
}
每次RelationshipState更改时都会创建一个关系实体。因此,对于任何一对用户,都会有许多关系对象。最近的一次是最新的

我试图查询表示特定用户对RelationshipState减少的任何关系对象

因此,所有用户的所有RelationshipObjects。日期比关系状态更高的日期晚


我发现在不迭代整个关系表的情况下,很难找到如何实现这一点。

在数据库中创建一个存储过程,该过程可以使用游标迭代项,并将它们与前面的项配对,然后过滤到递减状态

除此之外,您可以执行内部查询,查找每个项目的上一个值:

from item in table
let previous = 
    (from innerItem in table 
    where previous.date < item.Date 
    select innerItem)
    .Max(inner => inner.Date)
where previous.State > item.State
select item

尽管这看起来效率很低,但可能值得一试。也许,有了适当的索引、好的查询优化器和足够小的数据集,情况就不会那么糟了。如果速度慢得让人无法接受,那么用光标尝试存储的进程很可能是最好的。

首先,创建一个查询,返回所有用户组合和一个子进程,其中列出了所有状态更改。有关更多信息,请访问google LINQ Group By

然后使用你的收藏,通过查看最近两次状态变化,并查看是否下降,过滤掉所有你不想要的

下面是一个在LinqPad中作为C程序测试的示例:

public enum RelationshipState {
 state1 = 1,
 state2 = 2, 
 state3 = 3,
 state4 = 4
}
public class User {
   public int id {get;set;}
}
public class Relationship{
 public User User1{get;set;}
 public User User2{get;set;}
 public DateTime StateChangeDate {get;set;}

 //RelationshipState is an Enum with int values
 public RelationshipState State {get;set;}
}

void Main()
{


var rs=new List<Relationship>() {
 new Relationship{ User1=new User{id=1},User2=new User{id=2},StateChangeDate=DateTime.Parse("1/1/2013"),State=RelationshipState.state2},
 new Relationship{ User1=new User{id=1},User2=new User{id=2},StateChangeDate=DateTime.Parse("1/2/2013"),State=RelationshipState.state3},
 new Relationship{ User1=new User{id=1},User2=new User{id=3},StateChangeDate=DateTime.Parse("1/1/2013"),State=RelationshipState.state2},
 new Relationship{ User1=new User{id=1},User2=new User{id=3},StateChangeDate=DateTime.Parse("1/2/2013"),State=RelationshipState.state1},
 new Relationship{ User1=new User{id=2},User2=new User{id=3},StateChangeDate=DateTime.Parse("1/2/3013"),State=RelationshipState.state1}
};

var result=rs.GroupBy(cm=>new {id1=cm.User1.id,id2=cm.User2.id},(key,group)=>new {Key1=key,Group1=group.OrderByDescending(g=>g.StateChangeDate)})
 .Where(r=>r.Group1.Count()>1) // Remove Entries with only 1 status
 //.ToList() // This might be needed for Linq-to-Entities
 .Where(r=>r.Group1.First().State<r.Group1.Skip(1).First().State) // Only keep relationships where the state has gone done
 .Select(r=>r.Group1.First())  //Turn this back into Relationship objects
 ;

// Use this instead if you want to know if state ever had a higher state than it is currently
//  var result=rs.GroupBy(cm=>new {id1=cm.User1.id,id2=cm.User2.id},(key,group)=>new {Key1=key,Group1=group.OrderByDescending(g=>g.StateChangeDate)})
//   .Where(r=>r.Group1.First().State<r.Group1.Max(g=>g.State))
//   .Select(r=>r.Group1.First())
//   ;

result.Dump();
}

使用实际数据和预期结果进行任何尝试的一些代码示例都非常合适。非常好的响应。不需要收费表。我没有想过要把这群人转移到自己的地方。我试图用子查询来实现它。这要好得多