简体   繁体   English

列表视图中列出的选定组合框项目

[英]Selected Combo box items listed in a listview

I want to list selected combo box items in a listView by using LINQ to SQL. 我想通过使用LINQ to SQL列出listView中的选定组合框项目。 With my understanding the coding I have should work but it doesnt.(This coding should only display the CB items in the listview) 以我的理解,我拥有的编码应该可以工作,但是没有用(此编码应该只在列表视图中显示CB项目)

string pname = cbItem.SelectedItem.ToString();
listview.ItemsSource = DC.tblProducts.Where(p => p.ProductName == pname);

I basically want to make like a shopping cart but only to display the items that are selected thank you. 我基本上想做个购物车,但只想显示选中的项目,谢谢。

I think that your problem lies here: 我认为您的问题出在这里:

listview.ItemsSource = DC.tblProducts.Where(p => p.ProductName == pname);

From Microsoft's documentation on Enumerable.Where, emphasis mine ( https://msdn.microsoft.com/en-us/library/vstudio/bb534803%28v=vs.100%29.aspx ): 从Microsoft有关Enumerable的文档中,重点放在我的位置( https://msdn.microsoft.com/zh-cn/library/vstudio/bb534803%28v=vs.100%29.aspx ):

This method is implemented by using deferred execution. 通过使用延迟执行来实现此方法。 The immediate return value is an object that stores all the information that is required to perform the action. 立即返回值是一个对象,该对象存储执行操作所需的所有信息。 The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic. 不执行通过该方法所表示的查询,直到对象被通过直接调用其GetEnumerator方法或通过在Visual C#或对于每个使用 Visual Basic 中的foreach任一计数

The LINQ statement has been defined but not evaluated, and as such doesn't have a value that ItemsSource can figure out. LINQ语句已定义但尚未评估,因此没有ItemsSource可以确定的值。 Try: 尝试:

string pname = cbItem.SelectedItem.ToString();
var result = DC.tblProducts.Where(p => p.ProductName == pname);

var myList = new List<TblProductType>();
foreach(var v in result)
{
    myList.Add(v);
}
listview.ItemsSource = myList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM