Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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#_Linq - Fatal编程技术网

C# 基于或条件的Linq排序方式

C# 基于或条件的Linq排序方式,c#,linq,C#,Linq,这就是我的EF模型响应表的样子 Question_ID Responce 1 Agree 1 Agree 1 Tend to Agree 1 Tend to Agree 2 Agree 2 Agree 2 Tend to Agr

这就是我的EF模型响应表的样子

Question_ID         Responce

1                   Agree
1                   Agree
1                   Tend to Agree
1                   Tend to Agree

2                   Agree
2                   Agree
2                   Tend to Agree
2                   Tend to DisAgree

3                   DisAgree
3                   DisAgree
3                   Tend to DisAgree
3                   Tend to DisAgree
在上面的例子中,我想根据问题ID根据AgreeAgree进行排序,Agree应该包括在计算和不同意时倾向于同意。不同意应包括在计算时倾向于不同意

因此,同意订单如下

1 // (Got 4 Agree)
2 // (Got 3 Agree)
3 // (Got 0 Agree)
脱衣者应如下所示

3 // (Got 4 Disagree)
2 // (Got 1 Disagree)
1 // (Got 0 Disagree)
根据总响应次数将Linq模拟为订单。 我期待修改linq基于上述要求

db.Questions.OrderBy(x => x.Responces.Count).Select(y=>new { y.Question_ID,count= y.Responces.Count()}).ToList();

我建议您将响应列转换为如下枚举类型:

[Flags]
public enum Response
{
    Agree = 1,
    Unknown = 2,
    Disagree = 4,
    TendToAgree = Agree | Unknown,
    TendToDisagree = Unknown | Disagree
}
这样,您就可以检查所需的标志并计算条目:

var agreed = list.Where(x => x.QuestionID == 1).Count(x => x.Response.HasFlag(Response.Agree));
var disagreed = list.Where(x => x.QuestionID == 1).Count(x => x.Response.HasFlag(Response.Disagree));

不能同时按两个键分组。一个组需要一个唯一的密钥

但是,您可以做到以下几点:

db.Questions.GroupBy(x=>((x.Responce == "Agree")|(x.Responce == "Tend
to Agree")));

这会给你两个正确和错误的答案。在真正的群体中,所有的价值观都是一致的或倾向于一致的。在FALSE组中会有所有其他组。

您可以尝试db.Questions.OrderByx=>x.Responces.Countr=>r.ContainsDisAgree==FALSE.ToList;如果这能满足您的需求?您的需求与使用的LINQ不匹配。您将如何从组中获取个人ID?顺便说一句,Stack Overflow不是一个免费的编码服务,而且您似乎也没有尝试过任何东西。