Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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


all data is in my DT

Where DT is DataTable Object

my linq Query

C#
var distinctRows = (from DataRow dRow in DT.Rows select new { dRow["AdmissionNo"],dRow["StudentName"]}).Distinct();


plz help me in my query.
Posted
Updated 9-Jul-13 0:09am
v2

Hi,

Try:
C#
var distinctRows = (from DataRow dRow in dt.Rows select new { AdmissionNo = dRow["AdmissionNo"], StudentName = dRow["StudentName"]}).Distinct();

You didn't specify the member names:
http://msdn.microsoft.com/en-us/library/vstudio/bb397696.aspx[^]
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.
C#
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.
 
Share this answer
 
v3
Comments
Shambhoo kumar 9-Jul-13 6:28am    
Thanks for recommend.. i have also solve my problem.. Thanks.
Thomas Daniels 9-Jul-13 6:31am    
You're welcome!
hi i have my self

C#
var distinctRows = (from DataRow dRow in dt.Rows select new {col1=dRow["AdmissionNo"],col2=dRow["StudentName"]}).Distinct();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900