Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I have written one LINQ Statement in my code.Its a Select Distinct LINQ Statement as follows.
As the below Code is working.

string[] matchID = zteBbhRecords.Cast<IZteBbh>().Select(zteBbh => (zteBbh.CombinedMaster as ICombinedMaster).CellId.ToString()).GroupBy(item => item).Where(items => items.Count() > 1).Select(item => item.Key).Distinct().ToArray();


 if (zteBbhRecords.Count>0)
{
//// it will read the distict data
<pre lang="sql">zteBbhRecords.Cast<IZteBbh>().Select(zteBbh => (zteBbh.CombinedMaster as ICombinedMaster).CellId.ToString()).GroupBy(item => item).Where(items => items.Count() > 1).Select(item => item.Key).Distinct().ToArray();
}


My IF condition is not working.As I want to read the Distinct data but it read duplicate records as well .Please can you advice what changes i need to make in my if conditions.

Thanks
Harshal

EDIT (Matt T Heffron): Move code here from comment below, for readability.
C#
public IEntityCollectionBase GetZteBbhRecords(IEntityBase xmlConfiguration)
        {
            IDataReader dataReader = null;
            ExcelDataAccess excelDataAccess = null;
            IEntityCollectionBase zteBbhRecords = Kaizen2GEntityCreator.GetZteBbhEntityCollection();
            try
            {
                IXmlConfiguration xmlConfig = xmlConfiguration as IXmlConfiguration;
                Logger.Write("Inside ZteBbhDataManager.GetZteBbhRecords()", xmlConfig.LogTypeInfo);
                excelDataAccess = CommonDataManager.GetDataAccess(xmlConfig);
                dataReader = excelDataAccess.ExecuteReader();
                IZteBbh zteBbhEntity = null;
                while (dataReader.Read())
                {
                    zteBbhEntity = Kaizen2GEntityCreator.GetZteBbhEntity();
                    zteBbhEntity = CommonDataManager.GetEntityFromExcelRow(dataReader, xmlConfig, zteBbhEntity) as IZteBbh;
                    if (zteBbhEntity != null)
                    {
                        if ((zteBbhEntity.CombinedMaster as ICombinedMaster).CellId > 0
                          && !(zteBbhEntity.CombinedMaster as ICombinedMaster).CellName.ToUpper().Contains("TEST"))
                        {
                            //// match the record for same CellId.here i need to check
                            string[] matchID = zteBbhRecords.Cast<iztebbh>().Select(zteBbh => (zteBbh.CombinedMaster as ICombinedMaster).CellId.ToString()).GroupBy(item => item).Where(items => items.Count() > 1).Select(item => item.Key).Distinct().ToArray();
                            //// string result = ( from m in select m (zteBbhEntity.CombinedMaster as ICombinedMaster).CellId select m).Distinct().ToList();
                            if (matchID.Length > 0)
                            {
                                zteBbhRecords.Cast<iztebbh>().Select(zteBbh => (zteBbh.CombinedMaster as ICombinedMaster).CellId.ToString()).GroupBy(item => item).Where(items => items.Count() > 1).Select(item => item.Key).Distinct().ToArray();
                            }
 
                            if ((zteBbhEntity.CombinedMaster as ICombinedMaster).Circle.Trim().ToUpper() == "ROTN")
                            {
                                (zteBbhEntity.CombinedMaster as ICombinedMaster).Circle = "ROTN-ZTE";
                            }
                            else if ((zteBbhEntity.CombinedMaster as ICombinedMaster).Circle.Trim().ToUpper() == "AP")
                            {
                                (zteBbhEntity.CombinedMaster as ICombinedMaster).Circle = "AP-ZTE";
                            }
 
                            zteBbhEntity.FileLogId = xmlConfig.FileLogId;
                            zteBbhEntity.HandOverSuccessRate = zteBbhEntity.HandOverSuccessRate * 100;
                            zteBbhEntity.HandOverFailureRate = zteBbhEntity.HandOverFailureRate * 100;
                            zteBbhEntity.RandomAccessSuccessRate = zteBbhEntity.RandomAccessSuccessRate * 100;
                            zteBbhEntity.TbfSuccessRateUl = zteBbhEntity.TbfSuccessRateUl * 100;
                            zteBbhEntity.TbfSuccessRateDl = zteBbhEntity.TbfSuccessRateDl * 100;
                            zteBbhRecords.Add(zteBbhEntity);
                        }
                    }
                }
 
                Logger.Write("Exiting ZteBbhDataManager.GetZteBbhRecords()", xmlConfig.LogTypeInfo);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                dataReader.CloseDataReader();
                excelDataAccess.CloseConnection();
            }
 
            return zteBbhRecords;
        }
Posted
Updated 16-May-14 7:07am
v7
Comments
R Harshal 16-May-14 6:40am    
for any reference please have a look in code.
public IEntityCollectionBase GetZteBbhRecords(IEntityBase xmlConfiguration)
{
IDataReader dataReader = null;
ExcelDataAccess excelDataAccess = null;
IEntityCollectionBase zteBbhRecords = Kaizen2GEntityCreator.GetZteBbhEntityCollection();
try
{
IXmlConfiguration xmlConfig = xmlConfiguration as IXmlConfiguration;
Logger.Write("Inside ZteBbhDataManager.GetZteBbhRecords()", xmlConfig.LogTypeInfo);
excelDataAccess = CommonDataManager.GetDataAccess(xmlConfig);
dataReader = excelDataAccess.ExecuteReader();
IZteBbh zteBbhEntity = null;
while (dataReader.Read())
{
zteBbhEntity = Kaizen2GEntityCreator.GetZteBbhEntity();
zteBbhEntity = CommonDataManager.GetEntityFromExcelRow(dataReader, xmlConfig, zteBbhEntity) as IZteBbh;
if (zteBbhEntity != null)
{
if ((zteBbhEntity.CombinedMaster as ICombinedMaster).CellId > 0
&& !(zteBbhEntity.CombinedMaster as ICombinedMaster).CellName.ToUpper().Contains("TEST"))
{
//// match the record for same CellId.here i need to check
string[] matchID = zteBbhRecords.Cast<iztebbh>().Select(zteBbh => (zteBbh.CombinedMaster as ICombinedMaster).CellId.ToString()).GroupBy(item => item).Where(items => items.Count() > 1).Select(item => item.Key).Distinct().ToArray();
//// string result = ( from m in select m (zteBbhEntity.CombinedMaster as ICombinedMaster).CellId select m).Distinct().ToList();
if (matchID.Length > 0)
{
zteBbhRecords.Cast<iztebbh>().Select(zteBbh => (zteBbh.CombinedMaster as ICombinedMaster).CellId.ToString()).GroupBy(item => item).Where(items => items.Count() > 1).Select(item => item.Key).Distinct().ToArray();
}

if ((zteBbhEntity.CombinedMaster as ICombinedMaster).Circle.Trim().ToUpper() == "ROTN")
{
(zteBbhEntity.CombinedMaster as ICombinedMaster).Circle = "ROTN-ZTE";
}
else if ((zteBbhEntity.CombinedMaster as ICombinedMaster).Circle.Trim().ToUpper() == "AP")
{
(zteBbhEntity.CombinedMaster as ICombinedMaster).Circle = "AP-ZTE";
}

zteBbhEntity.FileLogId = xmlConfig.FileLogId;
zteBbhEntity.HandOverSuccessRate = zteBbhEntity.HandOverSuccessRate * 100;
zteBbhEntity.HandOverFailureRate = zteBbhEntity.HandOverFailureRate * 100;
zteBbhEntity.RandomAccessSuccessRate = zteBbhEntity.RandomAccessSuccessRate * 100;
zteBbhEntity.TbfSuccessRateUl = zteBbhEntity.TbfSuccessRateUl * 100;
zteBbhEntity.TbfSuccessRateDl = zteBbhEntity.TbfSuccessRateDl * 100;
zteBbhRecords.Add(zteBbhEntity);
}
}
}

Logger.Write("Exiting ZteBbhDataManager.GetZteBbhRecords()", xmlConfig.LogTypeInfo);
}
catch (Exception exception)
{
throw exception;
}
finally
{
dataReader.CloseDataReader();
excelDataAccess.CloseConnection();
}

return zteBbhRecords;
}
R Harshal 16-May-14 6:52am    
I have try a lot but then to my If condition is not working .Please advice me.

Thanks
Harshal
DamithSL 16-May-14 6:58am    
have you debug and check the values of matchID? is it distinct? and you need to groub by which field/fields?
R Harshal 16-May-14 7:02am    
Please go through the code once again ,as i have made some changes in it.

Thanks
Harshal
R Harshal 16-May-14 7:03am    
if you have any query feel free to ask .

1 solution

This part of the code doesn't make sense:
C#
                            //// match the record for same CellId.here i need to check
                            string[] matchID = zteBbhRecords.Cast<iztebbh>().Select(zteBbh => (zteBbh.CombinedMaster as ICombinedMaster).CellId.ToString()).GroupBy(item => item).Where(items => items.Count() > 1).Select(item => item.Key).Distinct().ToArray();
                            //// string result = ( from m in select m (zteBbhEntity.CombinedMaster as ICombinedMaster).CellId select m).Distinct().ToList();
                            if (matchID.Length > 0)
                            {
                                zteBbhRecords.Cast<iztebbh>().Select(zteBbh => (zteBbh.CombinedMaster as ICombinedMaster).CellId.ToString()).GroupBy(item => item).Where(items => items.Count() > 1).Select(item => item.Key).Distinct().ToArray();
                            }
</iztebbh></iztebbh>

The body of the if statement accomplishes NOTHING.
It re-constructs the same thing as matchID but then drops it on the floor, never to be seen again.
So what do you really want to do?

Also, there are two things wrong with the catch part of the try-catch-finally:
1. If you want the exception caught to continue up the execution stack for further handling, then just use:
C#
catch (Exception exception)
{
  throw;
}

explicitly giving the caught exception in the throw treats it as a new exception instead of continuing the same exception (different stack context)

2. Never catch an exception just to throw it. It isn't necessary to have a catch in order to have a finally.
 
Share this answer
 

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