Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have the following scenario:

CSS
public Class Form
{
public string Name {get; set;}
public string FormID {get; set;}
public string Path {get; set;}
}

List<Form>pdfdoc = new List<Form>{
new Form{ Name="FormA", FormID ="1", Path="D:/Temp/"},
new Form{ Name="FormB", FormID ="2", Path="D:/Temp/"},
new Form{ Name="FormC", FormID ="3", Path="D:/Temp/"},
new Form{ Name="FormC", FormID ="4", Path="D:/Temp/"},
new Form{ Name="FormD", FormID ="5", Path="D:/Temp/"},
new Form{ Name="FormD", FormID ="6", Path="D:/Temp/"},
new Form{ Name="FormE", FormID ="7", Path="D:/Temp/"},
new Form{ Name="FormF", FormID ="8", Path="D:/Temp/"},
new Form{ Name="FormG", FormID ="8", Path="D:/Temp/"},
new Form{ Name="FormH", FormID ="8", Path="D:/Temp/"},
};


I want that to check if the list contains FormC and FormD and then split the list into three lists, the first list containing FormA and FormB,second list containing FormC, FormC, FormD and FormD and the third list containing FormE,FormF,FormG and FormH. Is there any possible way?

The forms FormC and FormD can repeat any number of times at any position in the list. The total list may have more any number of forms. Please help.
Posted
Updated 29-Jun-13 0:26am
v2

Hope this is what you wanted.

C#
var x = from form in pdfdoc where form.Name == "FormA"  || form.Name == "FormB" select form;
var y = from form in pdfdoc where form.Name == "FormC" || form.Name == "FormD" select form;
var z = from form in pdfdoc where form.Name != "FormA" && form.Name != "FormB"
                    && form.Name != "FormC" && form.Name != "FormD"
                    select form;
 
Share this answer
 
Hi You can use a method like this:

C#
List<Form> list1, list2, list3;

SplitList(pdfdoc, out list1, out list2, out list3);


And this is the method:

C#
public static void SplitList(List<Form> mainList, out List<Form> firstList, out List<Form> secondList, out List<Form> thirdList)
{
    firstList = null;
    secondList = null;
    thirdList = null;
    if (mainList.All(l => l.Name != "FormC") || mainList.All(l => l.Name != "FormD"))
        return;
    firstList = CreateList(mainList, "FormA", "FormB");
    secondList = CreateList(mainList, "FormC", "FormD");
    thirdList = CreateList(mainList, "FormE", "FormF", "FormG", "FormH");
}

private static List<Form> CreateList(List<Form> list, params string[] formNames)
{
    var temp = new List<Form>();
    foreach (var s in formNames)
    {
        temp.AddRange(list.Where(l => l.Name == s));
    }

    return temp;
}


Or, you can use this method for thirdList:
C#
private static List<Form> CreateResidualList(List<Form> list, params List<Form>[] lists)
{
    return lists.Aggregate(list, (current, listTemp) => current.Where(l => !listTemp.Contains(l)).ToList());
}


And:
C#
thirdList = CreateResidualList(mainList, firstList, secondList);
 
Share this answer
 
v7

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