Search code examples
c#wpflinqobjectitems

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
}

So this is my class with some properties and constructors which I'm using to create a shop. I populate a listbox called lbxCategories, but when trying to get any information after selecting from the listbox and using a linq statement I get that error seen above.

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

    }

So when I run the code everything goes smoothly until I click on an item in the listbox which gives me this

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

Any help is greatly appreciated as I have projects due in a few days and need to get this out of the way


Solution

  • I'll be honest, it was a bit difficult to work out what you were trying to do with your code, but I think I have. Take a look at this:

    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();
        }
    }
    

    This will give you the ProductName of the first Item which has the selected category. If you actually want all the products matching that category, then you can take off FirstOrDefault from the end and it will give you an IEnumerable<String> of all the matching product names.