none
Query a list with condition from another list RRS feed

  • Question

  • I have a list hold some lot information. I have another list hold lot properities. I would like write a linq to get a result from list LotProperity which has LotID be found in list of lot. how can I do this?

    class Lot

    {

          public int id { get; set;}

    .....

    }

     

    class LotProperity

    {

        public string landlord { get ;set ;}

        public int LotID { get; set;}

    }

     

    Thanks,

    Monday, August 22, 2011 3:10 PM

Answers

  • The below code might help..

     class Program
      {
        static void Main(string[] args)
        {
          List<Lot> lotList = new List<Lot>() { new Lot() { id = 1 }, new Lot() { id = 2 } };
    
          List<LotProperity> lotPropery = new List<LotProperity>() { new LotProperity() { LotID = 1, landlord = "Sam" } };
    
          List<LotProperity> result = (from a in lotList
                         join b in lotPropery on a.id equals b.LotID
                         select b).ToList<LotProperity>();
    
        }
      }
      class Lot
      {
        public int id { get; set; }
      }
    
      class LotProperity
      {
        public string landlord { get; set; }
    
        public int LotID { get; set; }
      }
    

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, August 22, 2011 3:36 PM
  • LotProperity  list1;

    Lot list2;

    var result=from l1 in list1

                    let l2=list2.Select(l=>l.id)

                    where l2.Contains(l1.LotID)

                 select l1;




    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, August 22, 2011 3:37 PM
  • Contains is fastest if the inner list is an IQueryable. because it is evaluated late. If it is a static list then I don't think there are any difference in speed.
    Tuesday, August 23, 2011 10:10 AM

All replies

  • The below code might help..

     class Program
      {
        static void Main(string[] args)
        {
          List<Lot> lotList = new List<Lot>() { new Lot() { id = 1 }, new Lot() { id = 2 } };
    
          List<LotProperity> lotPropery = new List<LotProperity>() { new LotProperity() { LotID = 1, landlord = "Sam" } };
    
          List<LotProperity> result = (from a in lotList
                         join b in lotPropery on a.id equals b.LotID
                         select b).ToList<LotProperity>();
    
        }
      }
      class Lot
      {
        public int id { get; set; }
      }
    
      class LotProperity
      {
        public string landlord { get; set; }
    
        public int LotID { get; set; }
      }
    

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, August 22, 2011 3:36 PM
  • LotProperity  list1;

    Lot list2;

    var result=from l1 in list1

                    let l2=list2.Select(l=>l.id)

                    where l2.Contains(l1.LotID)

                 select l1;




    Best Regards,
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, August 22, 2011 3:37 PM
  • Thanks reply. which one has better performance? Contains or join.
    Monday, August 22, 2011 4:46 PM
  • Contains is fastest if the inner list is an IQueryable. because it is evaluated late. If it is a static list then I don't think there are any difference in speed.
    Tuesday, August 23, 2011 10:10 AM