Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
Hi,
I have the following scenario:
 
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 29-Jun-13 1:14am
Edited 29-Jun-13 1:26am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hope this is what you wanted.
 
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;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi
You can use a method like this:
 
List<Form> list1, list2, list3;
 
SplitList(pdfdoc, out list1, out list2, out list3);
 
And this is the method:
 
 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:
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:
thirdList = CreateResidualList(mainList, firstList, secondList);
  Permalink  
v7

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



Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 30 Jun 2013
Copyright © CodeProject, 1999-2014
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