Initializing Lookup

前端 未结 6 1558
轮回少年
轮回少年 2021-01-07 17:47

How do i declare a new lookup class for a property in the object initializer routine in c#?

E.g.

new Component() { ID = 1, Name = \"MOBO\", Category          


        
6条回答
  •  不知归路
    2021-01-07 18:25

    If you just need to return an empty ILookup for some reason you can return one from an empty dictionary. For example, to make an ILookup, you can use this:

    return new Dictionary().ToLookup(kvp => kvp.Key, kvp => kvp.Value);
    

    Unfortunately this is the most concise way I can see to do it without having to make a class that implements ILookup yourself.

提交回复
热议问题