Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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选择数组中的where EF导航属性值_C#_Entity Framework_Linq_Navigation Properties - Fatal编程技术网

C# LINQ选择数组中的where EF导航属性值

C# LINQ选择数组中的where EF导航属性值,c#,entity-framework,linq,navigation-properties,C#,Entity Framework,Linq,Navigation Properties,我有一个Web API方法,用于搜索名为“Patients”的EF对象。患者有一个名为“转诊”的导航属性(一个患者可以有多个转诊),每个转诊都有一个名为“ConsultantID”的整数属性 我想返回所有有转诊的患者,其ConsultantID值在一个名为“consultants”的定义数组内,但我无法理解所需的逻辑。目前我有以下内容,但它没有像我预期的那样工作,即转介。任何呼叫似乎都在执行“存在”呼叫,而不是我预期的加入行为 public List<HelperCode.DTO.Sear

我有一个Web API方法,用于搜索名为“Patients”的EF对象。患者有一个名为“转诊”的导航属性(一个患者可以有多个转诊),每个转诊都有一个名为“ConsultantID”的整数属性

我想返回所有有转诊的患者,其ConsultantID值在一个名为“consultants”的定义数组内,但我无法理解所需的逻辑。目前我有以下内容,但它没有像我预期的那样工作,即转介。任何呼叫似乎都在执行“存在”呼叫,而不是我预期的加入行为

public List<HelperCode.DTO.SearchResult> SearchPatients(string firstname, string surname, [FromUri] int[] consultants)
        {
            IQueryable<Patient> results = db.Patients;

            List<HelperCode.DTO.SearchResult> output = new List<HelperCode.DTO.SearchResult>();

            List<int> inputConsultants = consultants.OfType<int>().ToList();


            if (!String.IsNullOrEmpty(firstname)) { results = db.Patients.Where(c => c.FirstName.ToLower().Contains(firstname.ToLower())); }
            if (!String.IsNullOrEmpty(surname)) { results = results.Where(c => c.Surname.ToLower().Contains(surname.ToLower())); }


            if (consultants.Length > 0) { 
                results = results.Where(c => c.Referrals.Any(r => inputConsultants.Contains(r.ConsultantID ?? default(int)))); 
            }


            results = results.OrderBy(i => i.Surname);

            foreach (Patient p in results) {
                output.Add(new HelperCode.DTO.SearchResult(p));
            }

            return output;
        }
public List SearchPatients(字符串firstname,字符串姓氏,[FromUri]int[]consultants)
{
IQueryable结果=db.患者;
列表输出=新列表();
List inputConsultants=consultants.OfType().ToList();
如果(!String.IsNullOrEmpty(firstname)){results=db.Patients.Where(c=>c.firstname.ToLower().Contains(firstname.ToLower());}
如果(!String.IsNullOrEmpty(姓氏)){results=results.Where(c=>c.lasname.ToLower().Contains(lasname.ToLower());}
如果(0.Length>0){
results=results.Where(c=>c.Referrals.Any(r=>inputConsultants.Contains)(r.ConsultantID??default(int));
}
结果=结果.OrderBy(i=>i.姓氏);
foreach(结果中的患者p){
Add(newhelpercode.DTO.SearchResult(p));
}
返回输出;
}

开发人员的一个工具,代码工作正常,但输入值不正确。希望能对其他人有用。

开发人员是一个工具,代码工作正常,但输入值不正确。希望这一行对下面的人有用。

这一行的目的是什么
consultants.OfType().ToList()
?为什么您关心SQL查询是否使用
存在
加入
?你得到正确的结果了吗?如果是这样的话,问题是什么?@Ric这纯粹是为了将consultants数组转换成一个列表,以便以后调用.Contains方法。但是你使用一个int数组,过滤元素以只返回int,然后调用
ToList()
…@IvanStoev我不在乎。它似乎限制了结果集。如果我在数组中向它传递一个顾问ID值,它将返回220个结果,但如果我传入同一个ID和另一个3个顾问ID,它只返回28个-我希望它返回任何在数组中引用了ConsultantID的结果,这样会得到更多而不是更少的结果。这一行的目的是什么
?为什么您关心SQL查询是否使用
存在
加入
?你得到正确的结果了吗?如果是这样的话,问题是什么?@Ric这纯粹是为了将consultants数组转换成一个列表,以便以后调用.Contains方法。但是你使用一个int数组,过滤元素以只返回int,然后调用
ToList()
…@IvanStoev我不在乎。它似乎限制了结果集。如果我在数组中传递一个顾问ID值,它将返回220个结果,但如果我传递同一个ID和另一个3个顾问ID,它只返回28个-我希望它返回数组中有一个顾问ID引用的任何结果,因此期望得到更多而不是更少的结果。