linq all columns on a distinct field
-
i want to select all column for a table which have distinct column name that is select * from table1 where name is distict... Man! i don't even remember doing it in sql... i think it's something like select * from table where name is (select distinct(name) from table) or maybe not... i think, i have explained my problem... Please anybody help with the linq query Thanx in advance
haseeb
-
i want to select all column for a table which have distinct column name that is select * from table1 where name is distict... Man! i don't even remember doing it in sql... i think it's something like select * from table where name is (select distinct(name) from table) or maybe not... i think, i have explained my problem... Please anybody help with the linq query Thanx in advance
haseeb
var res = from x in ctx.X
let uniqueFieldYValues = ctx.X.Select(innerX => innerX.Y).Distinct()
where uniqueFieldYValues.Contains(x.Y)
select x;Eslam Afifi
-
var res = from x in ctx.X
let uniqueFieldYValues = ctx.X.Select(innerX => innerX.Y).Distinct()
where uniqueFieldYValues.Contains(x.Y)
select x;Eslam Afifi
hi, thanx for ur reply, i hope it works... thanx
haseeb
-
var res = from x in ctx.X
let uniqueFieldYValues = ctx.X.Select(innerX => innerX.Y).Distinct()
where uniqueFieldYValues.Contains(x.Y)
select x;Eslam Afifi
hi, just checked the query and it doesn't work... here is wat i did, created 9 objects of abstract class student(id,name,fathername) and saved it in a list 2 objects had same studentname, lets say "xyz"... created two gridview one for all students and other for distinct values ////////////////////code snippet/////////////////// List studentList = StudentList; List distinct = (from s in studentList let unique = studentList.Select(x => x.Name).Distinct() where unique.Contains(s.Name) select s).ToList(); gridview.DataSource = studentList; gridview.DataBind(); gridview2.DataSource = distinct; gridview2.DataBind(); //////////////////////////////////////////////////
-
hi, just checked the query and it doesn't work... here is wat i did, created 9 objects of abstract class student(id,name,fathername) and saved it in a list 2 objects had same studentname, lets say "xyz"... created two gridview one for all students and other for distinct values ////////////////////code snippet/////////////////// List studentList = StudentList; List distinct = (from s in studentList let unique = studentList.Select(x => x.Name).Distinct() where unique.Contains(s.Name) select s).ToList(); gridview.DataSource = studentList; gridview.DataBind(); gridview2.DataSource = distinct; gridview2.DataBind(); //////////////////////////////////////////////////
I tested my query against Linq-to-SQL and the Entity Framework. And sure it works with Linq-to-objects. The problem is in your code. List should be List. Make sure class name and properties are spelled correctly. Is StudentList a class or object? Abstarct class, I assume you are dealing with sub classes, you know you can't instantiate objects from abstract classes. I have limited experience in ASP.NET but I can't see any problem with the binding. If it doesn't solve the problem. Posting the exception details always helps.
Eslam Afifi