CareTaker22
CareTaker22

Reputation: 1310

Searching database with Linq

I am trying to search for data in my datagrid using the code sample below. I have had it working with the code looking a bit different, but I am now going to use async in my coding and I have tried to do so with the sample below, but have no idea how to change the code to work correctly.

    private async Task btnSearchSysproStock_Click(object sender, RoutedEventArgs e)
    {
        using (DataEntities DE = new DataEntities())
        {
            List<SSData> stockSearch = await (from a in DE.tblSysproStocks where 
                (a => txtSearchSysproStock.Text == string.Empty || a.StockCode.Contains(txtSearchSysproStock.Text)) //The error is in this line
                select new SSData
                {
                    SID = a.StockID,
                    SCode = a.StockCode,
                    SDescription = a.StockDescription,
                    SConvFactAltUom = (float)a.ConvFactAltUom,
                    ...
                }).ToListAsync();
            dgSysproStock.ItemsSource = stockSearch;
        }
    }

I am getting the following error:

Cannot convert lamba expression to type 'bool' because it is not a delegate type

Can anyone please help me to get this code that I am using to work. Thanks in advance! :)

Upvotes: 1

Views: 65

Answers (1)

har07
har07

Reputation: 89325

LINQ where clause expects bool expression, you don't need lambda here :

from a in ...
where txtSearchSysproStock.Text == string.Empty || 
      a.StockCode.Contains(txtSearchSysproStock.Text)
select ...

Upvotes: 3

Related Questions