在 linq 结果中插入额外数据,但不插入数据源或等待提交对上下文对象的更改
当我有一个类型化的数据表,其中包含从带有表适配器的 SQL Server 检索的信息时,我可以将临时数据插入到该数据表中,我只想在执行时使用该数据表,并且我不希望将其插入到数据库。
首先假设我有一个像这样的数据库表:
CREATE TABLE MyData
(
MyCol1 int,
MyCol2 nchar(10)
)
所以使用 SqlClient 对象这就是我所做的。
MyTypedDataSet.MyTypedDataTable myDataTable =
new MyTypedDataSet.MyTypedDataTable();
myDataAdapter.Fill(myDataTable);
myDataTable.NewMyTypedRow(value1, value2);
然后我可以将其绑定到一个控件,例如 ComboBox
myComboBox.DataSource = myDataTable;
myComboBox.DisplayMember = "myCol2";
myComboBox.ValueMember = "myCol1";
我如何对 LINQ to SQL 结果执行相同操作,例如:
var myQuery = from data in myContext.MyDatas
select myCol1, myCol2;
myComboBox.DataSource = myQuery;
看,我想在 myQuery
中添加该额外行,以便我可以将此数据绑定到我的组合框,但我不希望数据库中出现多余的行。
我通过调用 ToList()
方法来解决这个问题,然后添加我想要的数据,然后将此列表绑定到我的组合框,如下所示:
var myList = myQuery.ToList();
myList.Insert(0, new MyData() { myCol1 = value1, myCol2 = value2 });
myComboBox.DataSource = myList;
...但是我该如何执行此插入操作直接进入myQuery
?这是一个好的做法吗?或者我的解决方法是正确的做法?
PS:顺便说一句,当我过去将类型化数据表绑定到 ComboBox 时,我会使用架构生成的名称将值分配给 DisplayMember
和 ValueMember
,如下所示:
myComboBox.DisplayMember = myDataTable.myCol1Column.ColumnName;
myComboBox.ValueMember = myDataTable.myCol2Column.ColumnName;
我怎样才能从 myQuery
中提取此信息吗?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你所做的很好。
另一种方法是创建一个列表并插入其中,您可以创建一个包含一个元素的新列表并使用
Concat
方法连接剩余元素。这可以是 LINQ 单行代码。但你的方式可能更具可读性,所以也许坚持下去。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.您可以在数据绑定后将项目添加到 ComboBox 的项目集合中。
You could just add an Item to the ComboBox's Item Collection after it has been databound.