none
filter based on adress RRS feed

  • Question

  •  

    Hi

     

    I have the following code...

     

     

     

            public class Order
            {
                public string OrderName { get; set; }
                public DateTime DateOrdered { get; set; }
                public List<OrderRow> OrderRows { get; set; }
            }

            public class OrderRow
            {
                public string ArticleName { get; set; }
                public string FileName { get; set; }
                public OrderRowAdress Adress { get; set; }
            }

            public class OrderRowAdress
            {
                public string Attention { get; set; }
                public string Adress { get; set; }
                public string PostalCode { get; set; }
                public string City { get; set; }
            }

     

            private void button1_Click(object sender, EventArgs e)
            {
                Order MyOrder = new Order();
                List<OrderRow> Order_Rows = new List<OrderRow>();
                OrderRowAdress Order_RowAdress = new OrderRowAdress();

     

                Order_RowAdress = new OrderRowAdress();
                Order_RowAdress.Adress = "adress 1";
                Order_RowAdress.PostalCode = "555";
                Order_RowAdress.City = "NY";
                Order_RowAdress.Attention = "Tom";

                Order_Rows.Add(new OrderRow { ArticleName = "article 1", FileName = "file 1", Adress = Order_RowAdress });

                Order_RowAdress = new OrderRowAdress();
                Order_RowAdress.Adress = "adress 1";
                Order_RowAdress.PostalCode = "555";
                Order_RowAdress.City = "NY";
                Order_RowAdress.Attention = "Tom";

                Order_Rows.Add(new OrderRow { ArticleName = "article 2", FileName = "file 2", Adress = Order_RowAdress });

                Order_RowAdress = new OrderRowAdress();
                Order_RowAdress.Adress = "adress 3";
                Order_RowAdress.PostalCode = "777";
                Order_RowAdress.City = "Los Angeles";
                Order_RowAdress.Attention = "Rebecca";

                Order_Rows.Add(new OrderRow { ArticleName = "article 3", FileName = "file 3", Adress = Order_RowAdress });

                MyOrder.OrderName = "122-333-44";
                MyOrder.OrderRows = Order_Rows;
            }
           
           
    I need help on how to create a query that filter based on the "Order_RowAdress"in MyOrder. I need to find the the rows that have identical OrderRowAdress, so in this case the query would return 2 rows, the first row that have the two rows that have adress (adress = adress1, PostalCode = 555, City = NY, Attention = Tom) and the second row hold the single row with adress  (adress = adress3, PostalCode = 777, City = Los Angeles, Attention = Rebecca) 

     

    Hope this make sence and that someone could guide me.

     


    • Edited by magmo Saturday, July 2, 2011 8:14 AM typo
    Saturday, July 2, 2011 7:20 AM

Answers

  • Try it like this then.
    var query = from mo in MyOrderList        
          select new 
          {
            Order = mo,
            OrderAddress = from o in mo.OrderRows 
                    group o by new { o.Adress.Attention, 
                            o.Adress.Adress, 
                            o.Adress.PostalCode, 
                            o.Adress.City } 
          };
          
    foreach (var order in query)
    {
      foreach (var oa in order.OrderAddress)
      {
        Console.WriteLine("{0},\t{1}", order.Order.OrderName, order.Order.DateOrdered);
        foreach (var a in oa)
        {
          Console.WriteLine("\t{0},\t{1},\t{2},\t{3},\t{4},\t{5}",
            a.ArticleName, a.FileName,
            a.Adress.Attention, a.Adress.Adress,
            a.Adress.PostalCode, a.Adress.City);
        }
      }
    }        
    
    

    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by magmo Saturday, July 2, 2011 7:12 PM
    Saturday, July 2, 2011 6:45 PM

All replies

  • Hi magmo;

    The following query should give what you are looking for. 

     

    var query = from mo in MyOrder.OrderRows
          group mo by new { mo.Adress.Adress, mo.Adress.PostalCode, mo.Adress.City } into grpAddress
          select grpAddress.Select (a => a.Adress ).FirstOrDefault ( );
    
    foreach (var addr in query)
    {
      Console.WriteLine("{0},\t{1},\t{2},\t{3}", addr.Attention, addr.Adress,
        addr.PostalCode, addr.City);
    }
    
    // Will give the results
    Tom, adress 1, 555, NY       
    Rebecca, adress 3, 777, Los Angeles
    


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, July 2, 2011 3:27 PM
  • Hi Fernando

     

    Thanks that worked fine, is it also possible to get the complete order class so that in my case I could iterate through it and get this...

     

    iteration 1.

    the complete order class which then contains 2 order rows (article 1 & article 2) with identical adresses

    iteration 2.

    the complete order class which contain 1 order row (article 3)

    Saturday, July 2, 2011 4:12 PM
  • Hi magmo;

    Please read the comments in the code snippet.

     

    // This was added to have a collection of Order's
    List<Order> MyOrderList = new List<Order>();
    MyOrderList.Add(MyOrder);
    
    // Query the Collection of Orders
    var query = from mo in MyOrderList        
          select new 
          {
            Order = mo,
            OrderAddress = from o in mo.OrderRows 
                    group o by new { o.Adress.Attention, o.Adress.Adress, o.Adress.PostalCode, o.Adress.City } into grpAddress
                    select grpAddress.FirstOrDefault ( )
          };
          
    foreach (var order in query)
    {
      // Display the Order Information
      Console.WriteLine("{0},\t{1}", order.Order.OrderName, order.Order.DateOrdered);
      // Iterate through the addresses in the collection
      foreach (var a in order.OrderAddress)
      {
        Console.WriteLine("\t{0},\t{1},\t{2},\t{3}", a.Adress.Attention, a.Adress.Adress,
          a.Adress.PostalCode, a.Adress.City);
      }
    }
    


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, July 2, 2011 5:18 PM
  • Hi again

     

    Its not quite the result I was looking for, it only iterate once in the outer loop and then twice in the inner loop. I need the iteration to display it like this..

     

    iteration 1.

    the complete order class which then contains 2 order rows (article 1 & article 2) with identical adresses

    result: OrderName=122-333-44, and the 2 order rows that should belong here since they have the same adress. (Order row with article 1 and article 2)

    iteration 2.

    the complete order class which contain 1 order row (article 3)

    result: OrderName=122-333-44, and the 1 order row that should belong here. (Order row with article 3)

     

    Hope you see what I mean...

    Saturday, July 2, 2011 6:02 PM
  • Try it like this then.
    var query = from mo in MyOrderList        
          select new 
          {
            Order = mo,
            OrderAddress = from o in mo.OrderRows 
                    group o by new { o.Adress.Attention, 
                            o.Adress.Adress, 
                            o.Adress.PostalCode, 
                            o.Adress.City } 
          };
          
    foreach (var order in query)
    {
      foreach (var oa in order.OrderAddress)
      {
        Console.WriteLine("{0},\t{1}", order.Order.OrderName, order.Order.DateOrdered);
        foreach (var a in oa)
        {
          Console.WriteLine("\t{0},\t{1},\t{2},\t{3},\t{4},\t{5}",
            a.ArticleName, a.FileName,
            a.Adress.Attention, a.Adress.Adress,
            a.Adress.PostalCode, a.Adress.City);
        }
      }
    }        
    
    

    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by magmo Saturday, July 2, 2011 7:12 PM
    Saturday, July 2, 2011 6:45 PM
  • Hi Fernando

     

    Just what I needed, Thank you very much!

    Saturday, July 2, 2011 7:12 PM
  • Not a problem, glad I was able to help.

     


    Fernando

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, July 2, 2011 7:21 PM