Here is my problem, I have a multilang database shema. First I want a query with this result
French | English
Car | NULL
Etoile | Stars
NULL | Monkey
…
I’m pretty sure I will need to use A left join combinate with Pivot table.
Where I’m suppose to put this query. In a Partial class, or I need to use the Repository Pattern to hold the query.
I Have a lot of multilang table in the database. How can i make a generic query
Languages
LangID PK
LangName nvarchar(100)
Category
CatID Pk
IsActive Bit
CategoryText
CatID FK
CatName nvarchar(200)
LangID Int
It seems like you’re asking several questions, here. First, when you ask where to put the query, that’s really up to the design you’re following. A repository pattern seems like a reasonable idea for data access in a lot of cases, but I’ve no idea if it’s appropriate or not in your case. Are you already using repositories elsewhere, for example?
Second, are you looking for a working, “generic” query (ie. one that will work for any number of languages) in SQL, or through LINQ?
If in LINQ, please take a look at the answers to this question. They might be applicable: Pivot data using LINQ
If in SQL, and assuming you’re using SQL Server, you can indeed perform a pivot either with GROUP BY or, if you’re running a recent version, the PIVOT operator. However, neither case supports a dynamic number of languages – the requested columns for each must be declared explicitly. Like so:
Since the Pivot operator doesn’t support a dynamic statement like a subquery in the spreading argument (IN(English, French)), I’m not sure how to handle the generic case. Hopefully this will at least clarify your direction.