none
Accessing Properties in Related Table/Class RRS feed

  • Question

  • Hi All,

    KristoferA kindly answered a question for me recently regarding moving a LINQ2SQL query into my BLL .

    I now have a similar situation, although the relationship is a lot simpler, so I am wondering if it is overkill to create a class for the object. Basically, 99% of the objects properties reside in one table/class, with only one property other living in another table/class.

    Here is the code at the moment:

                using (DataClassesDataContext DB = new DataClassesDataContext())
                {
                    List<Attraction> recentlyUpdatedAttractions = DB.Attractions.OrderByDescending(a => a.dateUpdated).Take(_attractionsToShow).ToList();
                    DataListAttractions.DataSource = recentlyUpdatedAttractions; // < Line 4
                    DataListAttractions.DataBind();
                }

    This is getting a list of tourist attractions. What I need is the County that each one is in. The County is linked via countyID to the Counties table - Many attractions to each county.

    I have noticed that I can do the following (an extension of line 4 above)...

    ...recentlyUpdatedAttractions.SingleOrDefault().County1.county_name;

    I know that 'county_name' is a property of table 'Counties'

    So - My question is, how can I return Counties.County_name property along with the properties of Attractions table.

    I know I can do a join - this is straightforward for me, but - This means I am not using objects that reside in my BLL - I want my Linq2SQL class to be my BLL. Can I achieve this without building a separate 'attraction' class which is derived from a join of attractions and counties tables?

    I hope all this makes sense BTW - Its getting late and I've had a heavy day coding!! :)

    Cheers,

    Simon.

    Monday, September 14, 2009 8:39 PM

Answers

  • Hi smh_poole,

    In my opinion, to create a class for the object is the best way. Here're my suggestions:

    1. If your 'DataListAttractions' control receives anonymous type and there're no other operations, anonymous type is the choice.

    2. If anonymous type is not allowed and you need to return Counties.County_name property along with the properties of Attractions table, building a separate projection class is also my advice as KristoferA said, even if 'only one property other living in another table/class'.

    3. Have you ever noticed 'Customized Data Binding' in ASP.NET? According to your code, I figure it might help you. You can consult in the following link:

    http://forums.asp.net/


    Best regards,
    Charlie Lee

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by smh_poole Friday, September 18, 2009 2:54 PM
    Thursday, September 17, 2009 7:16 AM

All replies

  • Hi smh_poole,

    In my opinion, to create a class for the object is the best way. Here're my suggestions:

    1. If your 'DataListAttractions' control receives anonymous type and there're no other operations, anonymous type is the choice.

    2. If anonymous type is not allowed and you need to return Counties.County_name property along with the properties of Attractions table, building a separate projection class is also my advice as KristoferA said, even if 'only one property other living in another table/class'.

    3. Have you ever noticed 'Customized Data Binding' in ASP.NET? According to your code, I figure it might help you. You can consult in the following link:

    http://forums.asp.net/


    Best regards,
    Charlie Lee

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by smh_poole Friday, September 18, 2009 2:54 PM
    Thursday, September 17, 2009 7:16 AM
  • Charlie Lee, thanks for your reply - I figured this might be the case, just thought I would check if there is an easier way to cast two tables into another object, other than creating a projection class.

    I'll check out customised data binding too.

    Cheers,
    Simon.
    Friday, September 18, 2009 2:55 PM