We help IT Professionals succeed at work.

Whats wrong with my LINQ query  Trying to Find a Record in List

I have this Grid in my c# application

The Display Value in column 1  has an ID and Description  ( see  screen below)

Selected Record From Combo Box withing Grid

When i change values in column1 I want the description written into column2

I am trying to use LINQ  and a LAMDA Expression  . My script is as shown below

[private void dgvDetailsTable_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {

                switch (this.dgvDetailsTable.Columns[e.ColumnIndex].Name)
                {
                    case "dgvcmbarsaleditemid":
                        var result = ((List<InventoryItemModel>)dgvcmbarsaleditemid.DataSource).Find(x => x.INVITEMId.Equals(dgvcmbarsaleditemid.Selected));
                        this.dgvDetailsTable.CurrentRow.Cells[2].Value = result.INVITEMDescription;
                       

                        break;


                }
            }
        }[/code]

My database  field name is INVITEMId   and my   DataGrid  column 1 name is   dgvcmbarsaleditemid.


In the debugger  I can see that the 1188 records  were selected  and in the debugger i can see the loop search going on

See  Debugger  Screen below

Debugger Screen showing that I have ALL the 1188 records in my List

At the end of the looping it can not find the selected record and put it in the result variable even though the selected record
is on screen and coming from the database.  See  Error screen below

Error Screen with result  variable having null
Whats wrong with my LAMDA  expression  and how can i correct it

Thanks for any assistance

Olukay
Comment
Watch Question

Consultant
SILVER EXPERT
Are you sure that dgvcmbarsaleditemid.Selected is returning the right value for comparison?
Olukayode OluwoleSystems Analyst

Author

Commented:
No It wasnt

I found out the syntax isssue

Thanks