Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Purpose: To select a category in the combobox, to search for the information appropriate to the selected category in the textbox and finally to display it in the listbox. I want to write these codes with Linq.


C#
private void CBkategori_SelectedIndexChanged(object sender, EventArgs e)
        {
            string category = CBkategori.Text;
            query = "select name from items where category='" + category + "'";
            listbox(query);
        }

private void txtarama_TextChanged(object sender, EventArgs e)
        {
string category = CBkategori.Text;
        query = "select name from items where category='" + category + "' and name like '"+txtsearch+"%'";
        listbox(query);
}

private void listbox(string query)
{
    ListBox.items.clear();
    DataSet ds = fn.getData(query);
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++) ;
    {
        ListBox.items.Add(ds.Tables[0].Rows[i][0].ToString());
    }
}


What I have tried:

C#
string category= CBkategori.Text;
            item = new item();
            var query = from item in db.item
                        where item.name.Contains(kategori)
                        select item;
            listbox.DataSource = query.ToList();
Posted
Updated 16-Jan-21 1:10am
v2
Comments
Maciej Los 16-Jan-21 5:50am    
Your question is not clear. Can you provide more details?

1 solution

Here is an example from: Queries in LINQ to DataSet - ADO.NET | Microsoft Docs[^]
// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable products = ds.Tables["Product"];

IEnumerable<DataRow> productsQuery =
    from product in products.AsEnumerable()
    select product;

IEnumerable<DataRow> largeProducts =
    productsQuery.Where(p => p.Field<string>("Size") == "L");

Console.WriteLine("Products of size 'L':");
foreach (DataRow product in largeProducts)
{
    Console.WriteLine(product.Field<string>("Name"));
}

Another (recommend) way is using a BindingSource, see example here:
Creating a DataTable From a Query (LINQ to DataSet) - ADO.NET | Microsoft Docs[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900