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

C#
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="Form1", FormID ="1", Path="D:/Temp/"},
new Form{ Name="Form2", FormID ="2", Path="D:/Temp/"},
new Form{ Name="Form3", FormID ="3", Path="D:/Temp/"},
new Form{ Name="Form4", FormID ="4", Path="D:/Temp/"},
new Form{ Name="Form5", FormID ="5", Path="D:/Temp/"},
new Form{ Name="Form6", FormID ="6", Path="D:/Temp/"},
new Form{ Name="Form7", FormID ="7", Path="D:/Temp/"},
new Form{ Name="Form8", FormID ="8", Path="D:/Temp/"},
};


I want that to check if the list contains Form4 and then split the list into two lists, the first list containing Form1,Form2,Form3 and the other containing Form5,Form6,Form7,Form8. Is there any possible way?
Posted
Comments
Bernhard Hiller 17-Jun-13 2:21am    
Beware of the fact that the order of the list is not guaranteed! It may be different from the order of adding the elements...

C#
var index = pdfdoc.IndexOf(pdfdoc.Find(f => f.Name.Equals("Form")));

if (index > 0)
{
    var list1 = pdfdoc.GetRange(0, index);
    var list2 = pdfdoc.GetRange(index + 1, pdfdoc.Count - index - 1);
}
 
Share this answer
 
Comments
sanket.raj8 17-Jun-13 2:53am    
Thanks a lot. That works smooth.
Hi,
This is one of the way:


string[] str = new string[] { "1", "2", "3" };
List> method1 = method(str, pdfdoc);
List> method(string[] str, List aa)
{
List> dd = new List>();
List dd1 = (from s in str
from ss in aa
where s == ss.FormID
select ss).ToList();
List dd2 = aa.Except(dd1).ToList();
dd.Add(dd1);
dd.Add(dd2);
return dd;
}
 
Share this answer
 
Hi Saket,

Use LINQ, it's quite beautiful while doing such manipulations.

Use the code below:
--------------------------------
List<Form> List1 = (from docs in pdfdoc where (docs.FormID.CompareTo("4") < 0) select docs).ToList();

List<Form> List2= (from docs in pdfdoc where (docs.FormID.CompareTo("4") > 0) select docs).ToList();

--------------------------------


Hope it helps!

Happy Coding :)
 
Share this answer
 
v2
Comments
sanket.raj8 17-Jun-13 5:22am    
thanks...
I have given you the solution in c# windows application. So add c# windows application and write following code in code behind of form.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public string Name { get; set; }
        public string FormID { get; set; }
        public string Path { get; set; }
        List<Form1> pdfdoc;

        public Form1()
        {
            InitializeComponent();
        }

        public void Test()
        {
            pdfdoc = new List<Form1>{
                     new Form1{ Name="Form1", FormID ="1", Path="D:/Temp/"},
                     new Form1{ Name="Form2", FormID ="2", Path="D:/Temp/"},
                     new Form1{ Name="Form3", FormID ="3", Path="D:/Temp/"},
                     new Form1{ Name="Form4", FormID ="4", Path="D:/Temp/"},
                     new Form1{ Name="Form5", FormID ="5", Path="D:/Temp/"},
                     new Form1{ Name="Form6", FormID ="6", Path="D:/Temp/"},
                     new Form1{ Name="Form7", FormID ="7", Path="D:/Temp/"},
                     new Form1{ Name="Form8", FormID ="8", Path="D:/Temp/"},
                                    };
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Test();
            List<Form1> pdfdoc1 = new List<Form1>();
            List<Form1> pdfdoc2 = new List<Form1>();
            for (int i = 0; i < pdfdoc.Count; i++)
            {
                if (Convert.ToInt32(pdfdoc[i].FormID) < 4)
                {
                    pdfdoc1.Add(pdfdoc[i]);
                }
                else if (Convert.ToInt32(pdfdoc[i].FormID) > 4)
                {
                    pdfdoc2.Add(pdfdoc[i]);
                }
            }
        }
    }
}
 
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