Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. linq all columns on a distinct field

linq all columns on a distinct field

Scheduled Pinned Locked Moved LINQ
databasehelpcsharplinq
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haseeb_saeed
    wrote on last edited by
    #1

    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

    E 1 Reply Last reply
    0
    • H haseeb_saeed

      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

      E Offline
      E Offline
      Eslam Afifi
      wrote on last edited by
      #2

      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

      H M 2 Replies Last reply
      0
      • E 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

        H Offline
        H Offline
        haseeb_saeed
        wrote on last edited by
        #3

        hi, thanx for ur reply, i hope it works... thanx

        haseeb

        1 Reply Last reply
        0
        • E 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

          M Offline
          M Offline
          mark_me
          wrote on last edited by
          #4

          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(); //////////////////////////////////////////////////

          E 1 Reply Last reply
          0
          • M mark_me

            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(); //////////////////////////////////////////////////

            E Offline
            E Offline
            Eslam Afifi
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups