0

I have the following model to get data from database:

    public class CultureResource
    {
        public string KeyName { get; set; }
        public List<Resource> Resources { get; set; }
    }

    public class Resource
    {
        public string Value { get; set; }
        public string Culture { get; set; }
    }

Now my goal is to get data for a list of keys & culture where keys matches with CultureResource.KeyName & culture matches with Resource.Culture. How can I write a linq query on a IQueryable<CultureResource> to get a result of IList<Result> where Result may look like the following:

public class Result
{
    public string KeyName {get; set;}
    public string Value {get; set;}
}

I have tried the following Linq, but it throws $project or $group does not support {document}. error:

public IDictionary<string, string> GetLocalizedValueList(string[] keys, string culture)
{
    // returns IQueryable<CultureResource>
    var cultureResources = _repository.GetItems<CultureResource>();     

    /***** This is the query *********/
    var query = from cr in cultureResources
              from r in cr.Resources
              where keys.Contains(cr.KeyName) && r.Culture == culture
              select new { cr.KeyName, r.Value };
              
    var result = query.ToList()
              
    //return some data
}

FYI: my underlying database is mongo

1 Answer 1

0

How about this

        var query = cultureResources.SelectMany(cultureResource => cultureResource.Resources.Select(resource => new { cultureResource.KeyName, resource.Value })).ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

This one doesn't check against culture or key
Ok get you, can try this var query = cultureResources.Where(cultureResource => keys.Contains(cultureResource.KeyName)) .SelectMany(cultureResource => cultureResource.Resources.Where(resource=>resource.Culture==culture) .Select(resource => new { cultureResource.KeyName, resource.Value })).ToList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.