57057

Inserting extra data in a linq result but not to the data source or waiting for submiting changes on

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?

Answer1:

I think what you're doing is fine.

An alternative is instead of creating a list and inserting into it, you could create a new list containing one element and use the Concat method to concatenate the remaining elements. This can be a LINQ one-liner. But your way is probably more readable, so maybe just stick with that.

Answer2:

You could just add an Item to the ComboBox's Item Collection after it has been databound.

Answer3:

myComboBox.DataSource = new[] { new MyData { myCol1 = value1, myCol2 = value2 } } .Concat(myQuery);

Recommend

  • Program that sends an email notification if url is down
  • app.config transformations not applying
  • StoredProcedure in C#, passing parameters from text boxes
  • How to configure ProviderManifestToken for EF CodeFirst in a Connection String
  • SqlRoleProvider on IIS8 Express
  • What SqlCommand.Parameters.AddWithValue really does?
  • Columns with data-type prefix cause “Type X is not a defined system type” error
  • ASP.Net MVC 3 Configuration
  • Inserting extra data in a linq result but not to the data source or waiting for submiting changes on
  • Timeout expired, optimize query
  • Log4net buffer size not working
  • REST Web Service - Dynamic Query Parameters
  • How to match multiple corresponding values between columns in R
  • parse URL with regex in python
  • Bubble chart , scatter chart ,candlestick chart using core plot
  • mysql what is the right syntax for this conditional update statement
  • The type 'Microsoft.SqlServer.Types.SqlGeography' exists in both 'Microsoft.SqlServer
  • How to get a return value from a stored procedure in VB.NET
  • Local SQL Server installed by other user account, how to access from new user account
  • How to choose from two connection strings?
  • System.Data.SqlClient Namespace for MySQL?
  • How to get all values of text inputs with jQuery?
  • inter-group (between groups) combination of column A grouped by column B
  • Row_number() function for Informix
  • ONLY Visible Div form elements will get submitted
  • New dataframe with difference between first and last values of repeated measurements?
  • Get the pasted content on document on paste event
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • Knockout custom binding handler
  • jQuery: add elements until a particular height is reached
  • Debug.DrawLine not showing in the GameView
  • CSS Linear-gradient formatting issue accross different browsers
  • Retrieving value from sql ExecuteScalar()
  • jQuery show() function is not executed in Safari if submit handler returns true
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Matrix multiplication with MKL
  • How to get Windows thread pool to call class member function?
  • Is there any way to bind data to data.frame by some index?
  • Python/Django TangoWithDjango Models and Databases