以下是我的表格设置:
int | Design_1
0 | Design_A
1 | Design_B
2 | Design_C
以下是表单的代码:
var design = (from d in BikePartsDataContext1.handlebars
where d.@int == "0"
select d.Design_1);
this.textBox1.Text = design.ToString();
我要做的是让textBox1
文本具有Design_1
为0的行中@int
值的值。
一切正常,直到我将其作为textBox1
的文本值:
SELECT [t0].[Design 1] FROM [dbo].[handlebars] AS [t0] WHERE [t0].[int] = @p0
答案 0 :(得分:1)
我想你想要第一张基于Id的记录吗?
// at top of file so you can use the extension methods
using System.Linq;
// code
var design = (from d in BikePartsDataContext1.handlebars
where d.@int == 0 // i removed the quotes add them back if this is truely a string/sql varchar
select d.Design_1).Single(); // use single to ensure only 1 record will get selected
this.textBox1.Text = design; // design will now be the value of Design_1
一些注意事项:
SingleOrDefault
First
或FirstOrDefault
0
周围的引号,如果不是这样的话,请添加它们您也可以仅使用lambda表达式重写它:
this.textBox1.Text = BikePartsDataContext1.handlebars
.Where(x => x.@int == 0)
.Select(x => x.Design_1)
.Single();