locked
LINQ2SQL Looking for experts opinion. What would you do? RRS feed

  • Question

  • User1247667309 posted

    Hi!

    Im using LINQ2SQL and i have an object called Article. Its has 2 properties FinishText and SwedishText. The idea is to let the user choose language. Swedish is default. 

    I bind the objects to a listView and there is a label that takes the swedish language. When the user presses the Finish flag button i want the objects to reload and the finish text to show instead of the swedish. The problem is that the text label is bound to the SwedishText property in the mark up like this:

    <%#DataBinder.Eval(Container.DataItem, "SwedishText")%> 

    I can think of some ways to solve this, and i have one that doesnt work that well. My question is, what would be a good way to solve this? Im not so experienced so i know that there are lots of you out there that know how to do this much better. 

    Another problem i dont like my solution to is when i have an object that has a association with my Article object, like ArticleCategory. The ArticleCategoryId of my Article is, lets say 31, which is corresponds to the category "Movies". I dont want to display the category id but rather the name of the category itself (Movies). 

    So this is what i do:
    <%#GetCategoryNameFromId(DataBinder.Eval(Container.DataItem, "Article_Id"))%> 

    I call a method that recreates the artice object and from there creates the ArticleCategory and gets the name from it. Its a horrible solution cause it involves lots of trips to the Database. Especially since i do similar things with other properties. 


    Just wanted to hear what is a proper and good way to deal with these common but for me new tasks.


    Thank you!

    Friday, March 12, 2010 12:33 PM

Answers

  • User437720957 posted

    There are many ways to solve the localization part.  One of the easier is to not bind to the real classes, but use data transfer objects instead.


    var translatedArticles=from a in Articles select new {Id=a.Id, ArticleText = CurrentLanguage=="sv" ? a.SwedishText : a.FinnishText};


    Another option is to make the language selection inside the Article class

    public string Text
    {

       get  
       {
          if (CurrentLanguage=="sv")
             return SwedishText;
          else
             return FinnishText;

       }
    }

    Another problem i dont like my solution to is when i have an object that has a association with my Article object, like ArticleCategory. The ArticleCategoryId of my Article is, lets say 31, which is corresponds to the category "Movies". I dont want to display the category id but rather the name of the category itself (Movies). 

    If you have set up the association in L2S, why not just use the article.ArticleCategory.Name property?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 12, 2010 7:35 PM