Newb With Associations and Entity

2 posts, 0 answers
  1. Wayne
    Wayne avatar
    13 posts
    Member since:
    Sep 2012

    Posted 30 Jan 2014 Link to this post

    I have been using your OpenAccess for a little bit and so far every problem I have had was due to misuse so....here is another one!

    I have tow tables Customers and Address. Obviously the address table stores address for the customers. The Customer table as AutoInt ID called "id" which I store in the Address table as "CustID"...this allows the customer to have more than one address(shipping, billing, etc...) I have both tables in the Entity and I can pull data from either one. Now I want to bring up all the customers that have a specific Zip Code which is a field in the Address table\class. I created an association between the tables so that Customers has a Navigation Property "Addresses" which is a ilist of Address. Like I said I am new to this but how can I query the zip code of address and bring up the associated customers. They are tied by the CustID. I should also note that I am utilizing the Dynamic Ling Class for VB.NET. So what I am doing is creating a string with my query and then using that. My program allows the user to add up to five search terms and also choose the field theya re searching on! Works great when I stay inside the customer table but now I need to move outside it!

    I will Include some Code Snippets of what I am doing....plus this all may lead to a really dumb followup question! :)

    search = "Addresses.Contains(New Address With {.Zip = """ & FirstSearchItem.SearchTermText & """})"

    SearchResults = New ObservableCollection(Of Customer)(_mgrCustomer.Customers.Where(search))

    Thanks for any help!



  2. Boyan
    Admin
    Boyan avatar
    100 posts

    Posted 03 Feb 2014 Link to this post

    Hi Wayne,

    If I understood you correctly you would like to filter Customers by their Addresses' ZipCode. Could you please confirm that?
    If that is the case one possibility to achieve such behavior is to use the Dynamic LINQ equivalent to the Any method. For examples:

    Dim previousFilters = "Name.Length > 10"
    Dim zipCodeFilter = " AND Addresses.Any(ZipCode == ""1000"")"
    Dim queryString = previousFilters + zipCodeFilter
    Dim customers = dbContext.Customers.Where(queryString).ToList()

    This query would return all customers that have at least one address with ZipCode that is equal to 1000 (in addition to any previous filters).

    For more information and examples on how to use Dynamic LINQ with Telerik Data Access I would suggest you to take a look at our Samples Kit (under the LINQ category). 

    I hope this helps. Do not hesitate to contact us again if you have need any further assistance. 

    Regards,
    Boyan
    Telerik
    Telerik has recently updated some of its product names. OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  3. DevCraft banner
Back to Top