有2个列表框:患者和会话。 需要在第二个中显示已过滤(使用linq查询)信息,基于第一个中的多个选择。
注释字符串有效,但只允许在第一个列表框中选择一个项目。
我肯定知道它成功地将患者收集到List变量但是当我使用Contains时,第二个列表框是空的。
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataClasses1DataContext dbContext = new DataClasses1DataContext();
List<string> selectedItems = (from object o in listBox1.SelectedItems select listBox1.GetItemText(o)).ToList();
listBox2.DataSource = dbContext.Sessions
//.Where(pID => pID.PatientID == listBox1.GetItemText(listBox1.SelectedValue))
.Where(pID => selectedItems.Contains(pID.PatientID));
listBox2.DisplayMember = "Start";
listBox2.ValueMember = "SessionID";
}
Session和dbContext类定义:
public partial class Session : INotifyPropertyChanging, INotifyPropertyChanged
{
...
public int SessionID
{
...
}
public string PatientID
{
...
}
}
public partial class DataClasses1DataContext : System.Data.Linq.DataContext
{
...
public System.Data.Linq.Table<Session> Sessions
{
...
}
}
答案 0 :(得分:0)
我不知道你在listbox1中有什么类型的元素。我假设类型名为Session
:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataClasses1DataContext dbContext = new DataClasses1DataContext();
List<string> selectedItems = (from object o in listBox1.SelectedItems select listBox1.GetItemText(o)).ToList();
listBox2.DataSource = dbContext.Sessions
.Where(pID => listBox1.SelectedItems.OfType<Session>().Select(x => x.PatientID).Contains(pID.PatientID));
listBox2.DisplayMember = "Start";
listBox2.ValueMember = "SessionID";
}