Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have the following codes which will return multiple rows of records. But one of the drawback is that the records returned may be duplicated. For example, if the database return "Hello", it may return two "Hello". I can change the stored procedure adding a "Distinct" at the SELECT statement but is there any way of change it at the C# codes level instead of stored procedure?
Below are my codes:

C#
public CustomDataCollectionDto GetCustomDataCollectionQuestion(CustomDataCollectionDto customDataCollectionDto, long versionId)
        {
            CustomDataCollectionQuestionDtoList questionList = new CustomDataCollectionQuestionDtoList();
            Database db = DatabaseFactory.CreateDatabase();

            DbCommand selectCommand = db.GetStoredProcCommand("dbo.usp_CustomDataCollectionQuestionByVersionId_Select");
            db.AddInParameter(selectCommand, "p_CustomDataCollectionVersionId", DbType.Int64, versionId);
            db.AddInParameter(selectCommand, "p_CultureName", DbType.String, gsfParameterDto.Culture);

            using (IDataReader dataReader = db.ExecuteReader(selectCommand))
            {
                while (dataReader.Read())
                {
                    questionList.DtoList.Add(ParseDto<CustomDataCollectionQuestionDto>(dataReader));
                }

                customDataCollectionDto.CustomDataCollectionQuestionList = questionList;
            }

            return customDataCollectionDto;
        }
Posted

You're using DataReader to perform the select. This is great for efficiency as DataReader is a linear, readonly, forward only process. The drawback is that you can't tell if duplicate records exist until you look at the results.

So. Two ways:

One: Compare the given result with what has already been found:
(I have no idea what CustomDataCollectionQuestionDtoList is so I'll guess that it's the auto generated return type of the stored procedure)

C#
using (IDataReader dataReader = db.ExecuteReader(selectCommand))
  {
    while (dataReader.Read())
    {
      var next = ParseDto<customdatacollectionquestiondto>(dataReader);
      if(!questionList.DtoList.Any(item=>item.a==next.a&&item.b==next.b)
        questionList.DtoList.Add(ParseDto<customdatacollectionquestiondto>(dataReader));
    }
 
    customDataCollectionDto.CustomDataCollectionQuestionList = questionList;
  }
</customdatacollectionquestiondto></customdatacollectionquestiondto>




This is pretty good for small result sets but gets to be a real issue for larger sets.

The other method is to use the build in functionality of the completed set.
(again: no idea what CustomDataCollectionQuestionDtoList is so the available methods may differ)

C#
using (IDataReader dataReader = db.ExecuteReader(selectCommand))
  {
    while (dataReader.Read())
    {
      var next = ParseDto<customdatacollectionquestiondto>(dataReader);
      if(!questionList.DtoList.Any(item=>item.a==next.a&&item.b==next.b)
        questionList.DtoList.Add(ParseDto<customdatacollectionquestiondto>(dataReader));
    }
 
    customDataCollectionDto.CustomDataCollectionQuestionList = questionList.Distinct();
  }
</customdatacollectionquestiondto></customdatacollectionquestiondto>


This works great for Linq objects (hopefully CustomDataCollectionQuestionDtoList is at least an IEnumerable) but is most of your results are duplicates you have to be aware that questionList is going to be a lot larger than is needs to be.

Weigh up the options depending on your result set size and the percent of duplicates.

Hope that helps

have fun ^_^
 
Share this answer
 
Comments
Jamie888 27-May-15 6:48am    
Thank you sir, you really help me a lot!! Really appreciate it. Thank you.
Andy Lanng 27-May-15 6:52am    
A pleasure. I just wish I could cut down on the typo's >_<
Thanks for your response. It makes this 'hobby' worth it ^_^
Use the DataView ToTable function.

C#
var dataTable = new DataTable();
dataTable.Load(dataReader );
var dataView = new DataView(dataTable);
var distinctDataTable  = dataView.ToTable(true, "Column1", "Column2" ...);
 
Share this answer
 
Comments
Jamie888 27-May-15 6:49am    
Yes sir. I will give it a try on your suggested method. Thank you. :)
Tammam Koujan 27-May-15 7:07am    
You are welcome, for more details about ToTable method check the MSDN link:
https://msdn.microsoft.com/en-us/library/wec2b2e6(v=vs.110).aspx
Jamie888 28-May-15 1:38am    
Yes sir I will. Thank you again for your effort in helping me.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900