Click here to Skip to main content
12,066,952 members (57,868 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#2.0 C# ASP ASP.NET SQL ADO.NET , +
Am using one procedure to display search result. But the search result returning same values but i want to show it only once in my grid.
Consider, Procedure returns
Column A
A
A
A
B
C
C
and i want to show it in grid like,
Column
A
B
C

Procedure is correct but i need to filter values in C# code how can i do this?
Posted 4-Mar-13 0:45am
Edited 4-Mar-13 2:02am
v2
Comments
Tadit Dash 4-Mar-13 5:51am
   
I guess you have to select only distinct values from the procedure itself.
That would be good.

SELECT DISTINCT(Column_Name)
FROM
MyTable
Kavitha le 4-Mar-13 5:55am
   
In procedure i have more than one columns so i cant use this distinct over there. I need to do it from front end code.
Nandakishorerao 4-Mar-13 7:30am
   
you can use groupby clause
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Try this query in your datatable:
var uniqueColors =
               (from dbo in MainTable.AsEnumerable()
                 select dbo.Field<string>("Column").Distinct();


--Amit
  Permalink  
v2
Comments
Kavitha le 4-Mar-13 6:20am
   
Is this Linque??
_Amy 4-Mar-13 6:29am
   
Yes. :)
Kavitha le 4-Mar-13 7:00am
   
Here where i can give table name,field name etc?? please help
Kavitha le 4-Mar-13 7:18am
   
How can i call/use my procedure here?
_Amy 4-Mar-13 7:31am
   
MainTable is your datatable. You can use the same connection string which you was using to fetch the data from database.
Kavitha le 4-Mar-13 9:42am
   
what am using is,
Data = (*/data retrieved from datalayer*/)
in data layer am using data reader and taking data from DB using procedure
so how can i implement it here
Kavitha le 5-Mar-13 9:48am
   
:(
_Amy 5-Mar-13 22:51pm
   
What is the Data Type of your Data variable?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

If you can alter / create a procedure to use SELECT DISTINCT then do that.

If you cannot, and you are using a datasource that queries directly then you will have to add a pre-processing step to return a distinct table. Take a look at this:
C#... Selecting Distinct rows from DataTable[^]
  Permalink  
v2
Comments
Kavitha le 4-Mar-13 9:42am
   
Tried this, not working
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

May be this will Help you..!!
Declare string array which coumn you want to distinct
string[] TobeDistinct = { "Cloumn1",  "Cloumn2", "Cloumn3" };
call GetDistinctRecords fuction which will return you distinct record.

DataTable dtDistinct = GetDistinctRecords(dt, TobeDistinct);// dt which is to be distinct


Fuction is here:

private DataTable GetDistinctRecords(DataTable dt, string[] Columns)
    {
        DataTable dtUniqRecords = new DataTable();
        dtUniqRecords = dt.DefaultView.ToTable(true, Columns);
        return dtUniqRecords;
    }
  Permalink  
Comments
Kavitha le 4-Mar-13 8:54am
   
Here you have created two tables
dtDistinct
dtUniqRecords
is it correct?
OPees 4-Mar-13 23:12pm
   
Yes, Try this.
dtUniqRecords(DataTable) is returned from the fuction GetDistinctRecords(Which return type is DataTable)
which is assign to dtDistinct(DataTable)



Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

hi professional........


you should try DISTINCT query for this..........like

your Procedure returns
Column A
A
A
A
B
C
C

and for it,
Column
A
B
C
Try this--
SELECT DISTINCT column_name(s)
FROM table_name

it mean
SELECT DISTINCT Column_A from YourTableName.

for more go throw this link.....

SQL SELECT DISTINCT Statement[^]

Happy to help
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160208.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100