none
How to filter a list and store the selected items into another list RRS feed

  • Question

  • Hi,

    I am trying to find an answer for the below mentioned query i have. Please help me.

    I hve a class as below:

    public class CastComp{

    public int ID { get; set;}

    public string castid { get; set; }

    }

    and I have 2 lists as follows:

    List<CastComp> lstCast = new List<CastComp>();

    List<int> lstInt = new List<int>();

    and a below selecion. (here mylist will have values like 2,"e1223"...... )

    var mylist= from m in dataContext.Employees cs

               join cc in dataContext.Casts on cs.CastID equals cc.CastID

               select new { m.Cid,m.CastID; }

    lstInt = { 1,4,5,6,7,8,23 }

    Now I need to compare myList with lstInt and add the mathching records of lstInt ( say: 1,4,5,6,7,23) and mylist into the lstCast .

    eg: CastComp ccs = new CastComp();

         ie: Something like :  ccs.ID  and ccs.castid should be added into the ccsobject and eventuall ccs should get added into lstCast  object

    Please help me

    Regards

    -pep



    • Edited by pepcoder Friday, February 10, 2012 1:07 PM spellng correction
    Friday, February 10, 2012 1:02 PM

Answers

  • Hi pepcoder;

    The following code snippet should give you what you are looking for. Note the changes to your query.

    List<CastComp> lstCast = new List<CastComp>(); 
    List<int> lstInt = new List<int>() { 1,4,6,8,9 };    
    
    List<int> mylist = (from m in dataContext.Employees
                        join cc in dataContext.Casts on m.CastID equals cc.CastID
                        select new { m.Cid } ).ToList();
    
    lstCast = lstInt.Intersect(mylist).Select (c => new CastComp { ID = c } ).ToList();


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by pepcoder Sunday, February 12, 2012 5:59 PM
    Saturday, February 11, 2012 3:07 PM
  • Hi pepcoder;

    In order to use the Linq to Object Intersect function both objects that are to be used in that set operation must be of the same data type. Therefore I have wrapped the List<int> into a List<CastComp> just using the ID field. Also because the Intersect function will attempt to compare all fields for equality I needed to add a class to be used for only checking the ID field that can be passed to the Intersect function.

    List<CastComp> lstCast = new List<CastComp>( );
    List<int> lstInt = new List<int>( ) { 1, 4, 6, 8, 9 };
    
    List<CastComp> mylist = ( from m in dataContext.Employees
                              join cc in dataContext.Casts on m.CastID equals cc.CastID
                              select new CastComp() 
                              { 
                                  ID = m.Cid,
                                  CastId = m.CastNo 
                              } ).ToList();
    
    List<CastComp> temp = ( from t in lstInt
                            select new CastComp( ) 
                            { 
                                ID = t 
                            } ).ToList( );
    
    // Make sure that you use mylist first in the following statement because the items 
    // that are found to be equal are taken from this list
    lstCast = mylist.Intersect( temp, new MyCastCompToIntEqualityComparer( ) ).ToList( );
    
    
    // Your original CastComp class
    public class CastComp
    {
        public int ID { get; set; }
        public string CastId { get; set; }
    }
    
    // A IEqualityComparer for use in Linq Intersect function
    public class MyCastCompToIntEqualityComparer : IEqualityComparer<CastComp>
    {
    
        public bool Equals( CastComp x, CastComp y )
        {
            if( x.ID == y.ID )
                return true;
            else
                return false;
        }
    
        public int GetHashCode( CastComp obj )
        {
            return obj.ID.GetHashCode( );
        }
    
    }


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by pepcoder Sunday, February 12, 2012 5:59 PM
    Sunday, February 12, 2012 3:11 PM
  • Hi pepcoder;

    In order to use the Linq to Object Intersect function both objects that are to be used in that set operation must be of the same data type. Therefore I have wrapped the List<int> into a List<CastComp> just using the ID field. Also because the Intersect function will attempt to compare all fields for equality I needed to add a class to be used for only checking the ID field that can be passed to the Intersect function.

    List<castcomp> lstCast = new List<castcomp>( );
    List<int> lstInt = new List<int>( ) { 1, 4, 6, 8, 9 };
    
    List<castcomp> mylist = ( from m in dataContext.Employees
                              join cc in dataContext.Casts on m.CastID equals cc.CastID
                              select new CastComp() 
                              { 
                                  ID = m.Cid,
                                  CastId = m.CastNo 
                              } ).ToList();
    
    List<castcomp> temp = ( from t in lstInt
                            select new CastComp( ) 
                            { 
                                ID = t 
                            } ).ToList( );
    
    // Make sure that you use mylist first in the following statement because the items 
    // that are found to be equal are taken from this list
    lstCast = mylist.Intersect( temp, new MyCastCompToIntEqualityComparer( ) ).ToList( );
    
    
    // Your original CastComp class
    public class CastComp
    {
        public int ID { get; set; }
        public string CastId { get; set; }
    }
    
    // A IEqualityComparer for use in Linq Intersect function
    public class MyCastCompToIntEqualityComparer : IEqualityComparer<castcomp>
    {
    
        public bool Equals( CastComp x, CastComp y )
        {
            if( x.ID == y.ID )
                return true;
            else
                return false;
        }
    
        public int GetHashCode( CastComp obj )
        {
            return obj.ID.GetHashCode( );
        }
    
    }</castcomp></castcomp></castcomp></int></int></castcomp></castcomp>


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thanks for your help and effort Fernando..You rock!. great help..


    • Marked as answer by pepcoder Sunday, February 12, 2012 6:05 PM
    Sunday, February 12, 2012 6:03 PM

All replies

  • Hi,

    I don't understand you at all.

    Please could you give a more detailed sample with bothh list contents and the result you are looking for...

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.

    Friday, February 10, 2012 3:42 PM
  • Hi Ja,

    Thnx for the updates. My requirement is not so complex as you think :-)..Let me explain..

    List<CastComp> lstCast = new List<CastComp>(); -this is a list of CastComp where  I am going to add my final items.

    List<int> lstInt = new List<int>(); is a list which contains list of intergers as below

    {1,4,5,6,7,8,23 }

    and i have another collection below where  I will be having ids.

    var mylist= from m in dataContext.Employees cs

               join cc in dataContext.Casts on cs.CastID equals cc.CastID

               select new { m.Cid } ---This list would be like this .{1,2,5,6,4,3,8,9,12,12,44,45,4335,3423,345345 } Now I want to compare my firstlist with with the collection and store the matching values from both list to lstCast

    .alltogether  ...

    My first list would  be eg: {1,4,6,8,9} and my second list is eg:{1,4,3,5,6,7,8,8,9,6,4,3,3,5,6}

    i need to compare the above two lists and store the matching values into another list which is a list of castcomposites.I have declared it as : List<CastComp> lstCast = new List<CastComp>();.

    Hope u are clear now.Please help me.

    Saturday, February 11, 2012 1:07 PM
  • Hi pepcoder;

    The following code snippet should give you what you are looking for. Note the changes to your query.

    List<CastComp> lstCast = new List<CastComp>(); 
    List<int> lstInt = new List<int>() { 1,4,6,8,9 };    
    
    List<int> mylist = (from m in dataContext.Employees
                        join cc in dataContext.Casts on m.CastID equals cc.CastID
                        select new { m.Cid } ).ToList();
    
    lstCast = lstInt.Intersect(mylist).Select (c => new CastComp { ID = c } ).ToList();


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by pepcoder Sunday, February 12, 2012 5:59 PM
    Saturday, February 11, 2012 3:07 PM
  • Hi Fernando,

    Thanks a lot.That was an excellent suggestion.

    Now I have a query.What if my list below contians an int and a string as below.How can I compare it with listInt and store the values in lstCast.

    List<CastComp> lstCast = new List<CastComp>(); 
    List<int> lstInt = new List<int>() { 1,4,6,8,9 };    

    and

    List<which datatype I can use here> mylist = (from m in dataContext.Employees join cc in dataContext.Casts on m.CastID equals cc.CastID select new { m.Cid,m.CastNo } ).ToList();

    I wanna compare listInt and mylist and store the values in lstCast.

    I am new to LINQ.Sorry to ask ..Waiting for ur reply

    Regards

    pep

    • Marked as answer by pepcoder Sunday, February 12, 2012 5:59 PM
    • Unmarked as answer by pepcoder Sunday, February 12, 2012 5:59 PM
    Sunday, February 12, 2012 4:40 AM
  • Hi Fernando,

    Thanks a lot.That was an excellent suggestion.

    Now I have a query.What if my list below contians an int and a string as below.How can I compare it with listInt and store the values in lstCast.

    List<CastComp> lstCast = new List<CastComp>(); 
    List<int> lstInt = new List<int>() { 1,4,6,8,9 };    

    and

    List<which datatype I can use here> mylist = (from m in dataContext.Employees join cc in dataContext.Casts on m.CastID equals cc.CastID select new { m.Cid,m.CastNo } ).ToList();

    I wanna compare listInt and mylist and store the values in lstCast.

    I am new to LINQ.Sorry to ask ..Waiting for ur reply

    Regards

    pep

    Sunday, February 12, 2012 4:40 AM
  • Hi pepcoder;

    In order to use the Linq to Object Intersect function both objects that are to be used in that set operation must be of the same data type. Therefore I have wrapped the List<int> into a List<CastComp> just using the ID field. Also because the Intersect function will attempt to compare all fields for equality I needed to add a class to be used for only checking the ID field that can be passed to the Intersect function.

    List<CastComp> lstCast = new List<CastComp>( );
    List<int> lstInt = new List<int>( ) { 1, 4, 6, 8, 9 };
    
    List<CastComp> mylist = ( from m in dataContext.Employees
                              join cc in dataContext.Casts on m.CastID equals cc.CastID
                              select new CastComp() 
                              { 
                                  ID = m.Cid,
                                  CastId = m.CastNo 
                              } ).ToList();
    
    List<CastComp> temp = ( from t in lstInt
                            select new CastComp( ) 
                            { 
                                ID = t 
                            } ).ToList( );
    
    // Make sure that you use mylist first in the following statement because the items 
    // that are found to be equal are taken from this list
    lstCast = mylist.Intersect( temp, new MyCastCompToIntEqualityComparer( ) ).ToList( );
    
    
    // Your original CastComp class
    public class CastComp
    {
        public int ID { get; set; }
        public string CastId { get; set; }
    }
    
    // A IEqualityComparer for use in Linq Intersect function
    public class MyCastCompToIntEqualityComparer : IEqualityComparer<CastComp>
    {
    
        public bool Equals( CastComp x, CastComp y )
        {
            if( x.ID == y.ID )
                return true;
            else
                return false;
        }
    
        public int GetHashCode( CastComp obj )
        {
            return obj.ID.GetHashCode( );
        }
    
    }


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by pepcoder Sunday, February 12, 2012 5:59 PM
    Sunday, February 12, 2012 3:11 PM
  • Hi pepcoder;

    In order to use the Linq to Object Intersect function both objects that are to be used in that set operation must be of the same data type. Therefore I have wrapped the List<int> into a List<CastComp> just using the ID field. Also because the Intersect function will attempt to compare all fields for equality I needed to add a class to be used for only checking the ID field that can be passed to the Intersect function.

    List<castcomp> lstCast = new List<castcomp>( );
    List<int> lstInt = new List<int>( ) { 1, 4, 6, 8, 9 };
    
    List<castcomp> mylist = ( from m in dataContext.Employees
                              join cc in dataContext.Casts on m.CastID equals cc.CastID
                              select new CastComp() 
                              { 
                                  ID = m.Cid,
                                  CastId = m.CastNo 
                              } ).ToList();
    
    List<castcomp> temp = ( from t in lstInt
                            select new CastComp( ) 
                            { 
                                ID = t 
                            } ).ToList( );
    
    // Make sure that you use mylist first in the following statement because the items 
    // that are found to be equal are taken from this list
    lstCast = mylist.Intersect( temp, new MyCastCompToIntEqualityComparer( ) ).ToList( );
    
    
    // Your original CastComp class
    public class CastComp
    {
        public int ID { get; set; }
        public string CastId { get; set; }
    }
    
    // A IEqualityComparer for use in Linq Intersect function
    public class MyCastCompToIntEqualityComparer : IEqualityComparer<castcomp>
    {
    
        public bool Equals( CastComp x, CastComp y )
        {
            if( x.ID == y.ID )
                return true;
            else
                return false;
        }
    
        public int GetHashCode( CastComp obj )
        {
            return obj.ID.GetHashCode( );
        }
    
    }</castcomp></castcomp></castcomp></int></int></castcomp></castcomp>


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thanks for your help and effort Fernando..You rock!. great help..


    • Marked as answer by pepcoder Sunday, February 12, 2012 6:05 PM
    Sunday, February 12, 2012 6:03 PM