Read from CellSet using LINQ

1.5k views Asked by At
AdomdCommand cmd = new AdomdCommand(commandText, conn);
CellSet cs = cmd.ExecuteCellSet();

var result = from a in cs
             select new
             {...};

Is it possible to use LINQ to read from CellSet? I have tried using DataTable instead of CellSet but it's much slower (Example: I took a query that using DataTable takes ~45 sec to execute, but using CellSet it takes ~5 sec).

The error I get when trying to use LINQ:

Could not find an implementation of the query pattern for source type 'Microsoft.AnalysisServices.AdomdClient.CellSet'. 'Select' not found.

UPDATE:

I have tried Enrico's suggestion and so far it doesn't return any errors. Next problem is how to read a value from a cell. Here's what I have tried so far:

var result = from cell in cs.Cells.Cast<Cell>()
     select new searchResultsPolicy
     {
         PolicyNumber = ...
         InsuredName = cell.Field<string>("[Insured].[DBA Name].[DBA Name].[MEMBER_CAPTION]"),
         Agency = cell.Field<string>("[Agency].[Agency Name].[Agency Name].[MEMBER_CAPTION]"),
         Market = cell.Field<string>("[Market].[Market Name].[Market Name].[MEMBER_CAPTION]"),             
         Revenue = String.Format("{0:#,##0}", cell.Field<double?>("[Measures].[Revenue]") ?? 0),
         Premium = String.Format("{0:#,##0}", cell.Field<double?>("[Measures].[Premium]") ?? 0)
     };
1

There are 1 answers

3
Enrico Campidoglio On

The reason you get that error is that the CellSet class in itself doesn't implement IEnumerable<T>, which is required by LINQ.

Try executing the LINQ query over the CellSet.Cells property instead. That will return a CellCollection object, which implements IEnumerable. From there you can easily convert it to an IEnumerable<T> by using the Enumerable.Cast<T> method.

AdomdCommand cmd = new AdomdCommand(commandText, conn);
CellSet cs = cmd.ExecuteCellSet();

var result = from cell in cs.Cells.Cast<Cell>()
             select new
             { ... };

See also: