Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.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#_Asp.net Mvc_Linq - Fatal编程技术网

C# Linq查询返回的值不等于查询

C# Linq查询返回的值不等于查询,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我有一个有趣的问题。我正在对返回到模型的某些项运行linq查询。以下是我可以浏览的方法: 在PainCategory数据库中: Id Title CompanyId 1 Type 1 2 Priority 1 3 Likelihood 1 4 Type 2 5 Priority 2 6 Likelihood 2

我有一个有趣的问题。我正在对返回到模型的某些项运行linq查询。以下是我可以浏览的方法:

在PainCategory数据库中:

Id      Title       CompanyId
1        Type           1
2        Priority       1
3        Likelihood     1
4        Type           2
5        Priority       2
6        Likelihood     2
当我运行此查询时,我得到一个错误的比较:

 int compId = 0;
            //get project by id
            if (item.ProjectId != 0)
            {
                Userclient = new RestClient("http:www.website.com/id");
                var projReq = new RestRequest("project/{id}", Method.GET);
                projReq.AddUrlSegment("id", item.ProjectId.ToString());
                projReq.AddHeader("id", id);
                projReq.AddHeader("key", Key);
                projReq.RequestFormat = DataFormat.Json;
                var projResponse = Userclient.Execute(projReq) as RestResponse;
                ProjectDTO d = JsonConvert.DeserializeObject<ProjectDTO>(projResponse.Content);

                compId = d.CompanyId;
            }

            foreach(var i in defectsToReturn)
            {
                i.PainCategories = db.PainCategories.ToList().Where(p => p.CompanyId == compId);
            }
int compId=0;
//按id获取项目
如果(item.ProjectId!=0)
{
Userclient=newrestclient(“http:www.website.com/id”);
var projReq=new RestRequest(“project/{id}”,Method.GET);
projReq.AddUrlSegment(“id”,item.ProjectId.ToString());
projReq.AddHeader(“id”,id);
projReq.AddHeader(“键”,键);
projReq.RequestFormat=DataFormat.Json;
var projResponse=Userclient.Execute(projReq)作为restreponse;
ProjectDToD=JsonConvert.DeserializeObject(projResponse.Content);
compId=d.CompanyId;
}
foreach(依次为var i)
{
i、 PainCategories=db.PainCategories.ToList(),其中(p=>p.CompanyId==compId);
}
因此,
if(item.ProjectId!=0){
语句只对api执行RestSharp调用,并将compId设置为1


在我调试时的foreach语句中…compId的值也是1。但是i.PainCategories包含PainCategories表的所有6个元素,即使companyId的3个值被设置为2。知道为什么会发生这种情况吗?请提前感谢。

看来过滤器对您的
列表不起作用>.而不是:

i.PainCategories = db.PainCategories.ToList().Where(p => p.CompanyId == compId);
您能否在
列表转换之前应用过滤器

i.PainCategories = db.PainCategories.Where(p => p.CompanyId == compId).ToList();

试一试。

同样的结果,但感谢您的尝试。我注意到,当它通过foreach循环时,每次I.paincategories的计数为3。如果公司的所有6个都等于1,这可能会影响结果。