When I had a Typed DataTable with information retrieved from a SQL Server with a table adapter, I'm able to insert temporary data into that DataTable which I just want to use on execution time, and I don't want it to be inserted into the database.
First supose that for example I have a Database table like this:
CREATE TABLE MyData
(
MyCol1 int,
MyCol2 nchar(10)
)
So using SqlClient objects this is what I do.
MyTypedDataSet.MyTypedDataTable myDataTable =
new MyTypedDataSet.MyTypedDataTable();
myDataAdapter.Fill(myDataTable);
myDataTable.NewMyTypedRow(value1, value2);
I then could bind this to a control, e.g. a ComboBox
myComboBox.DataSource = myDataTable;
myComboBox.DisplayMember = "myCol2";
myComboBox.ValueMember = "myCol1";
How can I do the same with a LINQ to SQL Result such as:
var myQuery = from data in myContext.MyDatas
select myCol1, myCol2;
myComboBox.DataSource = myQuery;
See, I want to add that extra row in myQuery
, so that I can bind this data to my combo box, but I don't want that extra row in my database.
I worked around it by calling the ToList()
method, then I add the data I want, and then I bind this list to my ComboBox, like this:
var myList = myQuery.ToList();
myList.Insert(0, new MyData() { myCol1 = value1, myCol2 = value2 });
myComboBox.DataSource = myList;
... But how can I do this insert directly into myQuery
? would it be a good practice? or my workaround would be the right thing to do?
PS: By the way, when I used to bind typed data tables to ComboBoxes I would use the schema generated names to asign values to DisplayMember
and ValueMember
like this:
myComboBox.DisplayMember = myDataTable.myCol1Column.ColumnName;
myComboBox.ValueMember = myDataTable.myCol2Column.ColumnName;
How can I extract this information from myQuery
?