How to write a expression for a linq to sql property? -


My applications have already been exposed for long queries. I made a lot of effort to clarify my question as possible in a question. Please bear with me. ; O) Any help would be greatly appreciated!

I have classes branch and text:

  class group ID id text web group of a set of classical text int id string UK string NL string web string and other properties And also a bunch of other qualities  

I just want to show the identity of the branch and its details in the appropriate language. I do not want to have only one query (no extra round trips), which only receives two fields (not the whole object).

I get three solutions

through the object model in the query

  // good: no round trips // good: clean sql // Bad: currentUserLanguage parameter var lang = "NL" impossible to use; Var dbProject = new ProjectDataContext (); Query dbProject.GetTable & lt; b; Branch & gt; () New selection {b.ID, WebDescription = b.WebDescriptionObject.NL // & lt; - Problem}; Var text = query.First () Webdescription;  

After query by object model

  // Good: Any round trips (Curious loading of text object) / Good: Possible to use CurrentUserLanguage parameter // Bad: * Loads whole * branch and text object, not just two fields var lang = "NL"; Var dbProject = new ProjectDataContext (); Query dbProject.GetTable & lt; b; Branch & gt; () Select new {b.ID, WebDescription = b.GetWebDescriptionAsString (Lang)}; Var text = query.First () Webdescription;  

Use of an expression

  // Good: I think I'm on the right track / bad: it does not work: o (one Excerpt throws lang = "NL"; dbProject = new ProjectDataContext (); var query = dbProject.GetTable & ltb; branch & gt; () New selection {b.ID, WebDescription = b.GetWebDescriptionAsExpression (Lang) }; Var text = query.First () WebDescription;.  

Here are two methods for GetWebDescriptionAsString and GetWebDescriptionAsExpression

  public string GetWebDescriptionAsString (string string) {for Is the code of public expression & lt;; WebDescriptionObject.NL Return, if (Lang == "NL") WebDescriptionObject.NL Return, if WebDescriptionObject.FR return (Lang == "IT") return WebDescriptionObject.IT; WebDescriptionObject.UK Return; Function & Lt; branch, string> GetWebDescriptionAsExpression (string lang) {if (lang == "NL") return b = & gt; b.WebDescriptionObject.NL, if (la ng == "FR") return b = & Gt; b.WebDescriptionObject.FR; If (lang == "IT") returns B => B.WebDescriptionObject.IT; Return b = & gt; B.WebDescriptionObject.UK; }  

Really the best way to change the text structure without answering the question As the more generalized format:

  Text ID text translation id text ID lang text value  

Where there are many translations in each lesson, for each language A

This will be something like this query:

  var q = from dbProject branch. The branches add text to dbProject. Text on the branch. Text idid =. Add text to DbProject.TextTranslations on text.ID = translation.TextID where translation. Lang == long new {branch.id, webdescription = translation. Select TextView};  

There are other advantages of this approach, for example adding a new language will not change the model structure.


Comments