2

I would like to return the result of the LINQ query to the array, the list then look in this array for example with the letter "A". And return the list to the combobox containing all the letters "A".

My linq query:

var collectionName = (
    from row in repos.GetTable<Table_Names>() 
    select row.Name
    ).Distinct().ToArray();

And I do not know now how to search the array so that it will find all of them containing the letter "A".

Ultimately, I would like to search in the array instead of sending queries to the database. That would make the list in the combobox narrow down dynamically.

0

3 Answers 3

2

I think it's better to get the filtered collection, instead for perform search after getting the collection. So I suggest you to use a Where clause like the following to get filtered items only:

string searchString ="A";

from row in repos.GetTable<Table_Names>() 
where row.Name.Contains(searchString)
select row.Name;

Instead for Contains you can also try StartsWith if you want to get the collection of strings that starts with the given search text.

0

If you don't want to filter in the database, you can use linq to objects to further filter the collection in memory:

var filtered = collectionName.Where(item => item.Contains("A")).ToArray();
1
  • here, you dropped this: )
    – BurnsBA
    Aug 13, 2018 at 12:19
0

You can use SqlMethods.Like in LINQ query. Check the code below

private void comboBox1_TextChanged(object sender, EventArgs e)
    {
        comboBox1.DataSource = getItems(comboBox1.Text);
        comboBox1.DisplayMember = "Name";
        comboBox1.ValueMember = "ID";
    }
    public static List<ComboboxItem> getItems(string text)
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        try
        {
            List<ComboboxItem> Ilist = new List<ComboboxItem>();
            var query =  from x in context.testComboBoxes where SqlMethods.Like(x.name, '%' + text +'%') select x;
            foreach (var q in query)
            {
                ComboboxItem item = new ComboboxItem();
                item.ID = q.id;
                item.Name = q.name;
                Ilist.Add(item);
            }
            return Ilist;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
    public class ComboboxItem
    {
        public object ID { get; set; }
        public string Name { get; set; }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.