尝试 select 并从项目列表中输出数据,但收到的是这个而不是我想要的名称

Trying to select and output data from a list of Items but receiving this instead of the name I want

System.Linq.Enumerable+WhereSelectListIterator`2[ShopApp_Sem2_Project.Item,System.String]

public class Item
{
    #region Properties

    public int ItemID { get; set; }
    public string ProductName { get; set; }
    public string Manufacturer { get; set; }
    public string Category { get; set; }
    public double Price { get; set; }
    public string Info { get; set; }
    public string Image { get; set; }

    #endregion Properties

    #region Constructors
    public Item(int itemID, string product, string manufacturer, string category, double price, string info, string image)
    {
        ItemID = itemID;
        Manufacturer = manufacturer;
        Category = category;
        ProductName = product;
        Price = price;
        Info = info;
        Image = image;
    }
    #endregion Constructors
}

所以这是我的 class,其中包含我用来创建商店的一些属性和构造函数。我填充了一个名为 lbxCategories 的列表框,但是在从列表框中选择并使用 linq 语句后尝试获取任何信息时,我得到了上面看到的错误。

public partial class shopHome : Page
{
    string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Owner\Desktop\College Work\OOD\ShopApp_Sem2_Project\items.mdf;Integrated Security=True;";

    List<Item> allItems = new List<Item>();

    public shopHome()
    {
        InitializeComponent();
    }

    private void LbxCategories_Loaded(object sender, RoutedEventArgs e)
    {
        using (SqlConnection sqlCon = new SqlConnection(connectionString))
        {
            string query = "SELECT * FROM items ORDER BY itemID";

            sqlCon.Open();

            SqlCommand sqlCmd = new SqlCommand(query, sqlCon);

            SqlDataReader dr = sqlCmd.ExecuteReader();

            if(dr.HasRows)
            {
                while(dr.Read())
                {
                    Item newItem = new Item(Convert.ToInt32(dr[0]), dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), Convert.ToDouble(dr[4]), dr[5].ToString(), dr[6].ToString());

                    allItems.Add(newItem);
                }

                if(allItems != null)
                {
                    var results = allItems.Select(x => x.Category).Distinct();

                    lbxCategories.ItemsSource = results;
                }
            }
            sqlCon.Close();
        }
    }

    private void LbxCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string selectedItem = lbxCategories.SelectedItem as string;

        var results = allItems.Select(x => x.ProductName).ToString();

        test.Text = results; // this is just to test and see if I can get any input which isn't gibberish

    }

所以当我 运行 代码一切顺利,直到我点击列表框中的一个项目,它给了我这个

In the middle I just stuck a textblock to output the ProductName to

非常感谢任何帮助,因为我有几天后到期的项目,需要解决这个问题

老实说,弄清楚您要用您的代码做什么有点困难,但我想我做到了。看看这个:

private void LbxCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems != null && e.AddedItems.Count > 0)
    {
        string selected = (string)e.AddedItems[0];
        string productName = (from i in allItems where i.Category == selected select i.ProductName).FirstOrDefault();
    }
}

这将为您提供具有所选类别的第一个 ItemProductName。如果你真的想要 all 匹配该类别的产品,那么你可以从最后去掉 FirstOrDefault,它会给你一个 IEnumerable<String> 所有匹配的产品名字。