codeice / linqtoexcel

Automatically exported from code.google.com/p/linqtoexcel
0 stars 0 forks source link

How to use Distinct #45

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
code :

excel.AddMapping<SurveyDetails>(x => x.community, "Community *");

var community = (from row in excel.Worksheet<SurveyDetails>(worksheet)
                 where row.community!= ""
                 select row.community );

Scenario :

excel file has 1000 records and its one of column is 'community'
i am getting 1000 communities that are repeated in 'var community'
then i copy the var data to array and apply distinct on array that works fine 
but gives performance issue

i want to use distinct in above code

how to use it ?

Thankx in Advance 

please reply ....

version Details : 
Excel 2003  OS Vista

Original issue reported on code.google.com by deshmukh...@gmail.com on 7 Jun 2011 at 9:59

GoogleCodeExporter commented 8 years ago
Yea, LinqToExcel does not currently support the Distinct method, and 
unfortunately I do not have the extra bandwidth in my personal schedule to work 
on this feature. If you're interested in digging into the source code and 
submitting a patch, you can look at the VisitResultOperator() method in 
SqlGeneratorQueryModelVisitor.cs 
(https://github.com/paulyoder/LinqToExcel/blob/master/src/LinqToExcel/Query/SqlG
eneratorQueryModelVisitor.cs)

Original comment by paulyo...@gmail.com on 10 Jun 2011 at 9:13

GoogleCodeExporter commented 8 years ago
same problem.

how to use Distinct<>  

Original comment by rahul.si...@zeuslearning.com on 16 Nov 2011 at 10:18

GoogleCodeExporter commented 8 years ago
@rahul
get all the data in arraylist and apply distinct on it 
there is on way
if you are using dot net use LINQ

Original comment by deshmukh...@gmail.com on 17 Nov 2011 at 7:30

GoogleCodeExporter commented 8 years ago
There is a fix to support DISTINCT here:
https://github.com/assafshemesh/LinqToExcel
Still wasn't merged to main stream

Original comment by shemesh....@gmail.com on 17 Nov 2011 at 8:04

GoogleCodeExporter commented 8 years ago
I'm still working on merging assafshemesh code changes to use distinct. You can 
either use his changes or you can use the workaround that deshmukhshivkumar 
suggested by converting the results to a list and using distinct on the list. 

Here is an example of using distinct on a list

(from row in excel.WorkSheet<Company>() select row.Name).ToList().Distinct();

Original comment by paulyo...@gmail.com on 17 Nov 2011 at 12:32

GoogleCodeExporter commented 8 years ago
With version 1.5.5, LinqToExcel now supports the Distinct() method natively!

Let me know if anyone has issues with using it.

Original comment by paulyo...@gmail.com on 21 Nov 2011 at 4:11

GoogleCodeExporter commented 8 years ago
I wonder if something has changed in the latest version.  I tried to do 
Distinct() and it doesn't seem to work.  I even tried 
"myQuery.ToList().Distinct();" and that didn't do a thing.

My example has over a 1000 rows with different countries.  I want to get a 
unique country list.  I'm attaching the file for your review.  Here's my code 
for your review:
var masterDataXL = new ExcelQueryFactory(@"c:\temp\" + "DATA_TST.XLS");
var testList = (from x in masterDataXL.Worksheet("data_all")
                select x["Source"]).Distinct();

What am I doing wrong here?

Thanks for any help, in advance.

Original comment by denny.ma...@gmail.com on 1 May 2012 at 6:13

Attachments:

GoogleCodeExporter commented 8 years ago
To use Distinct in LinqToExcel, you have to use a class that corresponds to the 
row data.

public class WorksheetRow
{
    public string ColA { get; set; }
    public string ColB { get; set; }
}

var excel = new ExcelQueryFactory("worksheetFileName");
var distinctNames = (from row in excel.<WorksheetRow>WorkSheet()
                     select row.ColB).Distinct()

Original comment by paulyo...@gmail.com on 5 May 2012 at 11:42