Click here to Skip to main content
15,912,021 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Routine RemoveUnwantedRows works the same way, but passing List changes depends on data.
Is there anyway I can have one routine?
C#
private static List<gosooeamount> RemoveUnwantedRows(List<gosooeamount> lstData)
{

    List<gosooeamount> lstfoundEmpty = null;
    lstfoundEmpty = lstData.FindAll(c =>
                    c.Rec1_Amount == "0" &&
                    c.Rec1_Amount == "0");
    if (lstfoundEmpty.Count > 0)
    {
        foreach (GOSOOEAmount lstItem in lstfoundEmpty)
        {
            GOSOOEAmount foundGOS = null;
            foundGOS = lstData.FirstOrDefault(c =>
                            c.GOS_Seq == lstItem.GOS_Seq &&
                            c.Rec1_Amount != "0" &&
                            c.Rec1_Amount != "0");
            if (foundGOS != null)
            {
                lstData.Remove(lstItem);
            }
        }

    }

    return lstData;
}

private static List<gosftepositions> RemoveUnwantedRows(List<gosftepositions> lstData)
{

    List<gosftepositions> lstfoundEmpty = null;
    lstfoundEmpty = lstData.FindAll(c =>
                    c.Rec1_Amount == "0" &&
                    c.Rec1_Amount == "0");
    if (lstfoundEmpty.Count > 0)
    {
        foreach (GOSFTEPositions lstItem in lstfoundEmpty)
        {
            GOSFTEPositions foundGOS = null;
            foundGOS = lstData.FirstOrDefault(c =>
                            c.GOS_Seq == lstItem.GOS_Seq &&
                            c.Rec1_Amount != "0" &&
                            c.Rec1_Amount != "0");
            if (foundGOS != null)
            {
                lstData.Remove(lstItem);
            }
        }

    }

    return lstData;

}


[Edit]Code block added[/Edit]
Posted
Updated 5-Dec-12 6:34am
v2

1 solution

Why not use a generic type with a common interface.

C#
private static List<T> RemoveUnwantedRows<T>(List<T> lstData)  where T: Igos
        {
            List<T> lstfoundEmpty = null;
            lstfoundEmpty = lstData.FindAll(c =>
                            c.Rec1_Amount == "0" &&
                            c.Rec1_Amount == "0");
            if (lstfoundEmpty.Count > 0)
            {
                foreach (T lstItem in lstfoundEmpty)
                {
                    T foundGOS = lstData.FirstOrDefault(c =>
                                    c.GOS_Seq == lstItem.GOS_Seq &&
                                    c.Rec1_Amount != "0" &&
                                    c.Rec1_Amount != "0");
                    if (foundGOS != null)
                    {
                        lstData.Remove(lstItem);
                    }
                }
            }
            return lstData;
        }
 
Share this answer
 
Comments
SandhyaPillai 5-Dec-12 13:32pm    
I tried as follows, but how do u specify multiple ones for T?

private static List<t> RemoveUnwantedRows<t>(List<t> lstData) where T: GOSMOFAmount, GOSFTEPositions
BC @ CV 5-Dec-12 13:47pm    
I don't know what you mean by, "multiple ones for T". You just create classes that inherit the interface and pass the type thusly:

RemoveUnwantedRows<gosooeamount>(lstData)

If you are unable to inherit the interface because the classes are in an API you didn't create; you can either create your own derived class that inherits the gosooeamount and gosftepositions classes along with the interface or you can check which type the function is processing and cast it to that type. I suppose you could also use reflection but that's a lot of work.
SandhyaPillai 5-Dec-12 14:10pm    
Here is the scenario.
I created 2 classes as follows


public class GOSFTEPositions
{
public string GOS_Seq { get; set; }
public string Bud_Amount { get; set; }
public string Rec1_Amount { get; set; }
public string Rec2_Amount { get; set; }
public string Sequence { get; set; }
}

public class GOSMOFAmount
{
public string GOS_Seq { get; set; }
public string MOF_Code { get; set; }
public string Bud_Amount { get; set; }
public string Rec1_Amount { get; set; }
public string Rec2_Amount { get; set; }
public string Rider_Code { get; set; }
}

Then wrote a method but with different parameter
private static List<gosmofamount> RemoveUnwantedRows(List<gosmofamount> lstData)
private static List<gosftepositions> RemoveUnwantedRows(List<gosftepositions> lstData)

I may have to do the same with different List<t> too, hence looking for generic.
What would be the best way?
BC @ CV 5-Dec-12 14:23pm    
since the two classes are your creation, that makes it easy. You just inherit the common interface (ref as Igos in my solution) and pass the class type you want the function to process as shown in my previous comment.
SandhyaPillai 5-Dec-12 15:11pm    
can u give a code sample?

I created IGOS interface as follows
public interface IGOS{}

added inheritance to the classes
public class GOSMOFAmount :IGOS
public class GOSOOEAmount :IGOS

then defined following method
private static List<t> RemUnwantedRows<t>(List<t> lstData) where T: IGOS

But inside the method, when i do the following, it gives error
"T does not contain definition for Rec1_Amount "
lstfoundEmpty = lstData.FindAll(c =>
c.Rec1_Amount == "0" &&
c.Rec1_Amount == "0");

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