试图从项目列表中选择和输出数据,但接收到它而不是我想要的名称

问题描述 投票:-1回答:1

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
}

所以这是我的类,其中包含一些用于创建商店的属性和构造函数。我填充了一个名为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

非常感谢您的协助,因为我几天后就会有项目需要解决,因此请务必解决。>

System.Linq.Enumerable + WhereSelectListIterator`2 [ShopApp_Sem2_Project.Item,System.String]公共类Item {#region属性public int ItemID {get;组; }公用字符串ProductName ...

c# wpf linq object items
1个回答
0
投票

老实说,要弄清楚您要如何对代码进行处理有点困难,但是我想我已经做到了。看看这个:

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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.