none
Linq with multiple searches RRS feed

  • Question

  • HI

    I have a array of IDs and I want to have a foreach statement iterate each ID to an LINQ database search.   Each ID should be found in the database and be bound to a repeater.  All I get is the last search.  How do I get all the searches?

     Thanks

    Herb

     Int64[] AnimalIDs = new Int64[] {109, 115, 114,117};
    
            foreach (Int64 items in AnimalIDs)
            {
                var QueryList =                                          
                  from PersonX in AHData.Owners                        
                  join Animalx in AHData.Animals
                  on PersonX.OwnerId equals Animalx.OwnerId
                  where Animalx.AnimalId == items
                  where Animalx.AnimalKind == AnimalType
                  select new
                  {
                      Animalx,
                      PersonX,
                  };
                foreach (var Personxxx in QueryList)
                {
                    RepeaterDogList.DataSource = QueryList;
                    RepeaterDogList.DataBind();
                }
            }

    ="Dogs";
    
            
     
     
     
    Int64[] AnimalIDs =newInt64[] {109, 115, 114,117};
    
            
     
     
     
    foreach(Int64items inAnimalIDs)
    
            {
    
                
     
     
     
    varQueryList =                                          
    
                  
     
     
     
    fromPersonX inAHData.Owners                        
    
                  
     
     
     
    joinAnimalx inAHData.Animals
    
                  
     
     
     
    onPersonX.OwnerId equalsAnimalx.OwnerId
    
                  
     
     
     
    whereAnimalx.AnimalId ==items
    
                  
     
     
     
    whereAnimalx.AnimalKind ==AnimalType
    
                  
     
     
     
    selectnew
    
     
                  {
    
                      Animalx,
    
                      PersonX,
    
                  };
    
                
     
     
     
    foreach(varPersonxxx inQueryList)
    
                {
    
                    RepeaterDogList
     
     
     
    .DataSource =QueryList;
    
                    RepeaterDogList
     
     
     
    .DataBind();
    
                }
    
            }
    
        }
    





    Herb Rabe


    Thursday, October 9, 2014 4:30 PM

Answers

  • Hello AnimalHomeMaster,

    >> All I get is the last search.  How do I get all the searches?

    I looked at your code, and I see that you always binding the RepeaterDogList.DataSource with QueryList, I do not know what the RepeaterDogList is if it is similar with the DataGridView, from your current code, the data source would be always binding with the last query and as far as I know, a data binding control like DataGridView could only binding with only one data source.

    If you actually want to display records with the provided array, you could try with below code:

    List<int> ids = new List<int>() { 1, 2 };
    
    var querylist = (from o in db.Orders
    
                                 where ids.Contains(o.OrderID)
    
                                 select o).ToList();
    

    this.dataGridView1.DataSource = querylist.ToList();

    We do not need to use a foreach statement.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 10, 2014 3:05 AM
    Moderator

All replies

  • Hello AnimalHomeMaster,

    >> All I get is the last search.  How do I get all the searches?

    I looked at your code, and I see that you always binding the RepeaterDogList.DataSource with QueryList, I do not know what the RepeaterDogList is if it is similar with the DataGridView, from your current code, the data source would be always binding with the last query and as far as I know, a data binding control like DataGridView could only binding with only one data source.

    If you actually want to display records with the provided array, you could try with below code:

    List<int> ids = new List<int>() { 1, 2 };
    
    var querylist = (from o in db.Orders
    
                                 where ids.Contains(o.OrderID)
    
                                 select o).ToList();
    

    this.dataGridView1.DataSource = querylist.ToList();

    We do not need to use a foreach statement.

    Best Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 10, 2014 3:05 AM
    Moderator
  • This works.  I would never thought of doing it this way.  Most of my time is spent learning how to do something.  

    Thanks

    Herb


    Herb Rabe

    Friday, October 10, 2014 2:50 PM