COMPUTING ROW COUNT IN LINQ?

Pawan Shukla

Total Post:29

Points:203
Posted by  Pawan Shukla
C#  LINQ 
C#
 1018  View(s)
Ratings:
Rate this:

I am exploring a very messy SQL-Database via LINQPAD and I would like to get a list of all tables where at least one column is of type string. Furthermore, I would like to compute the count of distinct values for each column mentioned above.


I have tried to jumble something together, but my LINQ is rusty and anyway I have used it mainly for LINQ to Objects...


foreach(var table in Mapping.GetTables())

{

(

from dm in table.RowType.DataMembers

where dm.Type == typeof(string)

select new { dm.Name , dm.DbType , dm.Type , dm.MappedName, dm.IsPrimaryKey }

)

}

  1. Alex Leblois

    Post:67

    Points:471
    Re: Computing row count in linq?

    from table in Mapping.GetTables()

    from member in table.RowType.DataMembers

    where member.Type == typeof(string)

    let count = ExecuteQuery<int>(String.Format(

                    "SELECT COUNT(DISTINCT {0}) FROM {1}",

                    member.Name,

                    table.TableName)).FirstOrDefault()

    select new { table.TableName, member.Name, count }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!