How to return ILookup directly (without using a Dictionary->ILookup converter)

风格不统一 提交于 2019-12-11 09:46:31

问题


(Converting a Dictionary to ILookup is not very nice: How do I convert a Dictionary to a Lookup? )

I want to have an interface for my container class with the following method:

ILookup<Type, Entry> Query ( IEnumerable<QueryClause> query );

Each query clause specifies which and how many (and some more details) of a special kind of Entry should be taken out of the underlying container.

My implementation currently looks something like this:

var result = new Dictionary<Type, List<Entry>>();

foreach(var clause in query)
{
    var clauseResult = FulfillClause(clause);
    result.Add(clause.Type, clauseResult);
}

return result.ToLookup(); // here it is

Is there any chance this method my return ILookup directly? Unfortunately it does not support yield return.


回答1:


I'm not entirely sure why you have the dictionary in the first place. Does this work for you?

return query.ToLookup(clause => clause.Type, clause => FullFillClause(clause));

It doesn't comply with the ILookup<Type, Entry> interface but neither does the code you provide so I can't be certain about what you actually want.

Here's attempt after re-reading the question:

return query.SelectMany(c => FulfillClause(c).Select(r => new {Type=c.Type, Result=r}))
            .ToLookup(o => o.Type, o => o.Result);

It's a translation of @JonSkeet's linked answer.

To test without knowing the details of all the types & methods, I used this:

Func<List<int>> f = () => new List<int>() {1, 2, 3};
var query = new List<Type> {typeof (int), typeof (string)};

var l = query.SelectMany(t => f().Select(n => new {Type = t, Result = n}))
    .ToLookup(o => o.Type, o => o.Result);

If you control all the code, you could restructure some of it to enhance readability:

return query.SelectMany(c => c.Fulfill())
            .ToLookup(res => res.Type, res => res.Value);

...
// You will need to create the ClauseFulfillment type yourself
public IEnumerable<ClauseFulfillment> FulFill()
{
   var results = // whatever FulfillClause does
   foreach(var r in results)
      yield return new ClauseFulfillment {Type = Type, Result = r}; 
}


来源:https://stackoverflow.com/questions/20291147/how-to-return-ilookup-directly-without-using-a-dictionary-ilookup-converter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!