Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.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,我正在尝试将SP转换为linq查询 这是我现在的SP: UPDATE PatientAlertsSummary SET IsViewed =1 WHERE PatientID IN (SELECT PatientID FROM PatientTherapist WHERE TherapistID=@TherapistID) 我尝试查询linq代码,到目前为止,这是我的linq查询: 做这件事我遇到了一些困难 var query = from pas in context.PatientAl

我正在尝试将SP转换为linq查询

这是我现在的SP:

UPDATE PatientAlertsSummary
SET IsViewed =1 
WHERE  PatientID IN (SELECT PatientID FROM PatientTherapist WHERE TherapistID=@TherapistID)
我尝试查询linq代码,到目前为止,这是我的linq查询: 做这件事我遇到了一些困难

var query = from pas in context.PatientAlertsSummaries
                         where pas.PatientID.Contains(from pt in context.PatientTherapists where pt.TherapistID == therapistid  )
                            select pas;

                        foreach (var item in query)
                        {
                            item.IsViewed = true;
                        }

如何使其正确?

您可以将查询更改为联接表以检索所需的结果,然后相应地更新记录:

var query = from pas in context.PatientAlertsSummaries
join pt in context.PatientTherapists on pas.PatientID equals pt.PatientID
where pt.TherapistID == therapistid
select pas;

foreach (var item in query)
{
    item.IsViewed = true;
}

显然,这是未经测试的,并且基于您当前的sql/linq查询。

您可以这样做:

    public class PatientAlertsSummary
    {
        public bool IsViewed;
        public int PatientID;
    }

    public class PatientTherapist
    {
        public int PatientID;
        public int TherapistID;
    }

    public void UpdateIsViewed(PatientAlertsSummary summary)
    {
        summary.IsViewed = true;
    }

    [TestMethod]
    public void Test1()
    {
        //UPDATE PatientAlertsSummary
        //SET IsViewed =1 
        //WHERE  PatientID IN (SELECT PatientID FROM PatientTherapist WHERE TherapistID=@TherapistID)

        var therapistId = 1;

        var patientAlertsSummaries = new List<PatientAlertsSummary>() { 
            new PatientAlertsSummary(){PatientID = 1},
            new PatientAlertsSummary(){PatientID = 2},
            new PatientAlertsSummary(){PatientID = 3}
        };

        var PatientTherapists = new List<PatientTherapist>() { 
            new PatientTherapist(){PatientID = 1 , TherapistID = 1},
            new PatientTherapist(){PatientID = 2 , TherapistID = 1},
            new PatientTherapist(){PatientID = 3, TherapistID = 2}
        };

        var updatedPatinets1 = PatientTherapists.
            Where(o => o.TherapistID == therapistId).
            Join(patientAlertsSummaries,
            patientTherapist => patientTherapist.PatientID,
            patientAlertsSummary => patientAlertsSummary.PatientID,
            (o, p) => 
                {
                    UpdateIsViewed(p);
                    return p;
                }).ToList();

    }

我想您忘了在这个语句中指定pt in context.PATIENTTHERAPISTITS中的PatientId,其中pt.TherapistID==TherapistID。你应该把select PatientId放在末尾。如果我错了,请纠正我…select查询是获取数据而不是执行更新。只能在迭代结果时,才能在select中进行更新。