Is it possible to have a rollback for null values ​​in a LINQ query based on locale?

I am building a C # .NET 3.5 website. On this website, some content is read from an XML file. This XML file is saved from the Excel file, so basically Excel is a kind of content management system (I can not use the database in this situation). I use LINQ to XML to get collections of content objects, so I can work with them on web forms. I am just starting with LINQ, but I know the basics such as grouping and ordering.

I use the following code to read the contents in a collection (in this case, a list of videos):

string path = String.Format(server.MapPath("~/App_Data/Content.{0}.xml"), culture); XDocument source = cache.Get(path); bool boolValue = false; var query = from video in source.Descendants("Video") where video.Attribute("Id").Value != null /* Temporary values */ let featured = video.Attribute("IsFeatured").Value select new Video { Culture = culture, Id = video.Attribute("Id").Value, Name = video.Element("Name").Value, Description = video.Element("Description").Value, Category = video.Element("Category").Value, Url = video.Element("Url").Value, IsFeatured = Boolean.TryParse(featured, out boolValue) ? boolValue : false, Tags = video.Element("Tags").Value.Split(',').Select(t => t.Trim()).ToArray() }; return query.ToList(); 

In the current situation, there is one xml file for each culture. The mayor's disadvantage is that it will lead to a lot of redundant values. To make it more manageable, I will have only one xml file.

I would like to create a situation where strings with the same common identifier content, so I do not need to add redundant values. Is it possible to have a "recession" for null values?

Example: creating an xml file (excel)

If I want to read Swiss German content, the query will look for values ​​in which locale is "de-ch". If there are still empty values, the query will look for a "higher" language, in this case "de" and the last one, if there are still empty values, they will be taken from the default locale, which is "en".

Say I have a Product object. It has a name, description and price.

Start with English (en)

I will start with the culture of "en" because it is my standard culture. In excel, I would create a line:

  • Locale = ru
  • Name = Product 1
  • Description = Description in English
  • Price = 100

Then German (de) Then I need a translation into German, so I add one more line, but I leave the name and price empty because they are the same.

  • Locale = de
  • Name =
  • Description = German Description
  • Price =

Then Swiss German (de-ch) Here I only want to add a price, because it has a different currency.

  • Locale = de-ch
  • Name =
  • Description =
  • Price = 222

The result I'm aiming for in LINQ

When the culture is de-ch, the returned object in LINQ will have the following values:

  • Locale = de-ch
  • Name = Product 1 (from en)
  • Description = German Description (from de)
  • Price = 222 (from de-ch)

I think I can do it in regular C # with some loops, but how would you do it in Linq? Maybe someone can give some direction on how I can achieve this in LINQ.

+4
source share
1 answer

In the interest of clarity for those who are going to accept this, I would suggest downloading it via linq, as you put it in a data structure that can handle this form of backup.

There will be no or little cost compared to any Frankenstein method that Linq is likely to need.

And in my experience, I have not seen anything that will do this. Someone else could ..

+3
source

Source: https://habr.com/ru/post/1390233/


All Articles