I have a DataGridViewCellCollection
and want to read the values from the cells. Later I want to create a new object, pass in these values as constructor parameters and add them to a list.
List<Connection> connections = new List<Connection>();
for (int i = 0; i < dataGridView.Rows.Count; i++)
{
DataGridViewCellCollection cells = dataGridView.Rows[i].Cells; // current cellrow
int firstValue = (int)cells[1].Tag; // convert to int
int? secondValue = (int?)cells[0].Value; // convert to nullable int
connections.Add(new Connection(firstValue, secondValue));
}
return connections;
Connection
itself represents this struct
internal struct Connection
{
public Connection(int firstID, int? secondID)
{
FirstID = firstID;
SecondID = secondID;
}
public int FirstID { get; private set; }
public int? SecondID { get; private set; }
}
I would like to rewrite this code with Linq
but how can I select specific multiple values and cast the result to an object?