简体   繁体   中英

Enumeration in RavenDb multi-map reduce returns null instead of an empty list

I'm using a multi-map reduce to make a unique collection of strings, "Block" names, for each enumerable "ClientName". (I like using multi-maps since I can make sure all of my Client have a value in the query, even if it's empty).

I want the result of my index to return an empty list instead of null when no blocks are found in any products for that client. Here is my index.

public class Client_Blocks
    : AbstractMultiMapIndexCreationTask<Client_Blocks.Result>
{
    public class Result
    {
        public Client.Names ClientName { get; set; }
        public IEnumerable<string> Blocks { get; set; }
    }

    public Client_Blocks()
    {
        AddMap<Product>(products =>   
            from product in products
            where product.Details != null
            where product.Details.Block != null
            select new
            {
                ClientName = product.ClientName,
                Blocks = new List<string>{product.Details.Block}
            });

        AddMap<Client>(clients =>
            from client in clients
            select new
            {
                ClientName = client.Name,
                Blocks = new List<string>()
            });

        Reduce = results => from result in results
            group result by result.ClientName
            into g
            select new
            {
                ClientName = g.Key,
                Blocks = g.Any() ? 
                         g.SelectMany(x => x.Blocks).Distinct() :
                         new List<string>()
            };
    }
}

I also tried using g != null instead of g.Any() and only having the g.SelectMany(...) . The index works fine, but returns null for "Blocks" in the result instead of an empty list when there are no products with Blocks for a particular client. Is it possible to return an empty list here from an index? Is it an intended consequence for the list to always be null when empty?

您的reduce会存储FitBlocks,但是您的地图正在使用Blocks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM