我正在尝试构建一个小型应用程序"使用复选框作为搜索方法。当一次只检查一个盒子时,它正常工作,但是当我检查时 多个框,然后它仅显示最后一个复选框的结果。我一直在尝试调试它,我可以看到每个数据源都被覆盖 遭遇。任何可以帮助提供关于如何"存储"数据并没有在每个"如果"声明?
C#
private void UpdateBinding()
{
wodListBox.DataSource = null;
wodListBox.DataSource = Exercise;
wodListBox.DisplayMember = "ExerciseID";
}
private void Checkboxes()
{
DataAccess db = new DataAccess();
if (BarBellCheckBox.Checked)
{
Exercise = db.GetExercise(BarBellCheckBox.Text);
}
if (WallBallCheckBox.Checked)
{
Exercise = db.GetExercise(WallBallCheckBox.Text);
}
if (KettleBellCheckBox.Checked)
{
Exercise = db.GetExercise(KettleBellCheckBox.Text);
}
if (BarCheckBox.Checked)
{
Exercise = db.GetExercise(BarCheckBox.Text);
}
UpdateBinding();
}
public class DataAccess
{
public List<ExerciseModel> GetExercise(string ExerciseEquipment)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("WodDB")))
{
return connection.Query<ExerciseModel>("dbo.GetEquipmentType @ExerciseEquipment", new { ExerciseEquipment = ExerciseEquipment }).ToList();
}
}
}
T SQL
CREATE PROCEDURE [dbo].[GetEquipmentType]
@ExerciseEquipment nvarchar(50)
AS
BEGIN
SELECT ExerciseName, ExerciseEquipment
FROM dbo.ExerciseName
INNER JOIN dbo.ExerciseEquipment
ON dbo.ExerciseName.ExerciseID = dbo.ExerciseEquipment.ExerciseID
WHERE ExerciseEquipment = @ExerciseEquipment
END
答案 0 :(得分:1)
快速:您在每个if
- 阻止结果中覆盖结果。
那么如何解决这个问题?
由于您的“练习”类型为List<ExerciseModel>
,因此您可以在if
语句之前初始化它:
Exercise = new List<ExerciseModel>();
以及稍后在if块中使用AddRange()
Exercise.AddRange( db.GetExercise(BarBellCheckBox.Text) );