【问题标题】:C# Show selected row data in a new gridview 1-1 relationshipC#在新的gridview 1-1关系中显示选定的行数据
【发布时间】:2022-01-01 09:58:54
【问题描述】:

我正在创建一个简单的患者应用程序。 1-1 患者与病史记录的关系。当应用程序启动时,会显示患者。选择一个患者并单击按钮时,应显示该患者的病史。

我的问题是显示所有病史,而不是显示特定患者。

          private void PopulateGrid()
    {
        var records = _db.GeneralMedicalHistories.Select(q => new
        {
            PatientNumber = q.GeneralMedicalHistoryID,
            MaritalStatus = q.MartialStatus,
            Education = q.Education,
            BloodType = q.BloodType,
            Pregenancies = q.Pregnancies,
            Tobacco = q.Tobacco,
            Alcohol = q.Alcohol,
            Drug = q.Drug,
            MedicalHistoryNotes = q.MedicalHistoryNotes,

        }).ToList();

        gvDemographics.DataSource = records;
        gvDemographics.Columns["PatientNumber"].HeaderText = "Patient#";
        gvDemographics.Columns["MaritalStatus"].HeaderText = "Marital Status";
        gvDemographics.Columns["BloodType"].HeaderText = "Blood Type";
        gvDemographics.Columns["MedicalHistoryNotes"].HeaderText = "Medical History Notes";
        //Hide the column for ID. Changed from the hard coded column value to the name, 
        // to make it more dynamic. 
        gvDemographics.Columns["PatientNumber"].Visible = true;
    }

    

实现 .Where() 后的新代码。 C_PatientID 是病史表中的外键,与患者表中的主键匹配

 var record = _db.GeneralMedicalHistories.Where(q => q.C_PatientID == q.Patient.PatientID).Select(q => new
        {
            PatientNumber = q.GeneralMedicalHistoryID,
            MaritalStatus = q.MartialStatus,
            Education = q.Education,
            BloodType = q.BloodType,
            Pregenancies = q.Pregnancies,
            Tobacco = q.Tobacco,
            Alcohol = q.Alcohol,
            Drug = q.Drug,
            MedicalHistoryNotes = q.MedicalHistoryNotes,

        }).ToList();

【问题讨论】:

    标签: c# asp.net linq


    【解决方案1】:

    您已选择要在网格视图中显示的元素,但尚未设置任何类型的条件来过滤查询结果。

    为此,首先获取与您要显示的记录关联的 ID — 我将其命名为 patientID。将.Where(p => p.PatientNumber == patientID) 扩展方法添加到您的查询应该将您的结果过滤到具有patientID 的患者。

    .Where() documentation.

    【讨论】:

    • 我知道我需要某种过滤,这是问题所在,因为我发现的所有文档都只使用一个 lambda 表达式。我在将 .where 合并到我的代码中时遇到问题。 @Scrinary
    • 非常感谢,这在某种程度上很有帮助。我已经实现了 '.Where()' 但我仍然得到所有记录,因为我使用了两个不同的表。我将在线程中包含我的代码。
    • @MoAI 我认为问题在于这样的Where 语句将包括在您的第二个表中具有相应外键的任何患者。您在应用程序中必须做的是确定用户选择了哪个键,将其命名为patientID,并通过将当前的Where 语句替换为.Where(q => q.C_PatientID == patientID) 将其包含在您的代码中。
    • 请多多包涵。 ` var patientID = gvDemographics.SelectedCells.Cast() .Select(cell => cell.RowIndex) .Distinct();Here is my selected cell to determine the row selected. When I compare it with q.C_PatientID == patientID` 我收到 `CS0019 Operator '==' cannot be应用于 'int' 和 'IEnumerable` 类型的操作数
    • 当您使用var 声明变量时,编译器通过查看表达式右侧返回的类型来推断类型。这就是为什么你定义的patientID 的类型最终是IEnumerable<int>。据推测,您只对一条记录感兴趣,因此请尝试将 .FirstOrDefault() 添加到 linq 查询的末尾以仅返回一个 int。此外,您的查询返回行索引而不是患者 ID。如果没有看到其余代码,很难确定,但您可能希望将 lambda 表达式替换为 cell => cell.Value
    猜你喜欢
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    相关资源
    最近更新 更多