0

I'm not familiar with Arrays at all hence I need help on how to exclude a value from being added into the below variable:

Dim columns = PL_DGV.Columns.Cast(Of DataGridViewColumn)().ToArray()

I was thinking about the .Where() function but unsure how to apply it to the above array of DataGridViewColumns:

Dim columns = PL_DGV.Columns.Cast(Of DataGridViewColumn)().Where("Name <> column_name").ToArray()

I might need to exclude few values from it therefore it would be nice if maybe the column_name bit could be a variable of multiple values?

3
  • If you're not familiar with arrays then you should be searching the web for information on arrays. That said, this question has nothing to do with arrays. That Where method doesn't accept a String and any research on the web would have shown you that. Look for examples of its use and do what they do. You can probably click it in the code editor and press F1 to go straight to the documentation and find examples there. You should be providing a Lambda expression so that is the next thing you should be researching. Commented Jul 27, 2021 at 13:21
  • Well, I'm not saying the Where has to be used, I simply tried it because I remember seeing it somewhere in some code and also because I know SQL well and its a common syntax to use whenever filtering values, which is kind of what I am trying to do - filter values before they are assigned to the columns variable. I have tried researching how to remove/filter/exclude a value from an array, or even converting to string then back to array after removing value, none worked hence the question on here... Commented Jul 27, 2021 at 13:31
  • That Where method is definitely the right option in that case, but you're not calling it correctly. You need to provide an appropriate Lambda expression, so you should find out what that would be. That Lambda can be anything that evaluates to a Boolean, so there's no requirement to use an equality comparison. If you want to determine whether a value is contained in a list of values then you can use Contains in your Lambda. Commented Jul 27, 2021 at 14:43

1 Answer 1

1

LINQ:

Dim columns = (From c As DataGridViewColumn In Me.PL_DGV.Columns Where c.Name <> "column_name").ToArray

To exclude multiple values:

Dim namesToExclude = {"Name1", "Name2"}.ToList
Dim columns  = (From c As DataGridViewColumn In Me.PL_DGV.Columns Where Not namesToExclude.Contains(c.Name)).ToArray
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.