How do you dispose of IDisposableobject create inside of a Linq expression?

前端 未结 2 1658
一整个雨季
一整个雨季 2021-01-19 01:42

Linq allows you to create new object inside of a query expression. This is useful when you have classes that encapsulate generation of a list. I’m wondering how you dispose

2条回答
  •  情深已故
    2021-01-19 01:59

    OK; I'm hoping this is coincidence, but: SelectMany; combining IDisposable and LINQ, which (with the custom SelectMany implementation) would allow:

        var foo =
            from r in Enumerable.Range(1, 3)
            from gen in new Generator()
            from g in gen.Gen(r)
            select g;
    

    (note that I'm assuming there is a sensible reason to do this per r)

    or with the Using() extension method (instead of SelectMany):

        var foo =
            from r in Enumerable.Range(1, 3)
            from gen in new Generator().Using()
            from g in gen.Gen(r)
            select g;
    

提交回复
热议问题