问题描述
我正在使用此代码从表中获取distnict值,但我的代码却报错,所以请帮我处理我的代码,我在linq中更大.
我的表格数据
i m using this code to get distnict value from a table, but i get error on my code so plz help me on my code, i m bigger in linq.
my table data
serialno AdmissionNo StudentName subjecID Marks
1 A001 Amit Kumar 1 89
2 A002 Rahul Kumar 1 93
3 A001 Amit Kumar 2 87
4 A002 Rahul Kumar 2 79
所有数据都在我的DT中
其中DT是DataTable对象
我的linq查询
all data is in my DT
Where DT is DataTable Object
my linq Query
var distinctRows = (from DataRow dRow in DT.Rows select new { dRow["AdmissionNo"],dRow["StudentName"]}).Distinct();
请帮助我进行查询.
plz help me in my query.
推荐答案
var distinctRows = (from DataRow dRow in dt.Rows select new { AdmissionNo = dRow["AdmissionNo"], StudentName = dRow["StudentName"]}).Distinct();
您没有指定成员名称:
http://msdn.microsoft.com/en-us/library/vstudio/bb397696.aspx [^ ]
You didn''t specify the member names:
http://msdn.microsoft.com/en-us/library/vstudio/bb397696.aspx[^]
如果您未在匿名类型中指定成员名称,则编译器将为匿名类型成员提供与用于初始化它们的属性相同的名称.您必须为使用表达式初始化的属性提供名称,如上例所示.在下面的示例中,匿名类型的属性的名称为Color和Price.
If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them. You must provide a name for a property that is being initialized with an expression, as shown in the previous example. In the following example, the names of the properties of the anonymous type are Color and Price.
var productQuery =
from prod in products
select new { prod.Color, prod.Price };
foreach (var v in productQuery)
{
Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}
但是没有用于初始化匿名类型的属性,因此会出现错误.
希望对您有所帮助.
But there''re no properties being used to initialize the anonymous type, so you get an error.
Hope this helps.
var distinctRows = (from DataRow dRow in dt.Rows select new {col1=dRow["AdmissionNo"],col2=dRow["StudentName"]}).Distinct();
这篇关于无效的匿名类型成员声明匿名类型成员必须使用成员分配简单名称或成员访问权限进行声明.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!