none
LINQ qusetion RRS feed

  • Question

  • hi

     public static IEnumerable<MCircle> ToSimple2DCircles(this IEnumerable<MCircle> circles, List<MLayer> workingLayers, int id)
            {
                var collection = from c in circles
                                 from wl in workingLayers
                                 where c.Layer.Name == wl.LayerName
                                 select new { c.Radius, wl.LayerCategory };
    
                foreach (var elm in collection)
                {
                    id++;
                    yield return new MCircle
                        (id, elm.Radius, elm.LayerCategory);
                }
            }


    is it possible to get rid of foreach,

    and do something like

                                 from c in circles
                                 from wl in workingLayers
                                 where c.Layer.Name == wl.LayerName
                                  yield return new MCircle (id, elm.Radius, elm.LayerCategory);


    • Edited by essamce Wednesday, February 26, 2020 8:17 PM
    Wednesday, February 26, 2020 8:16 PM

Answers

  • Hi,

    Sorry I did not have time to test or optimize it, but try something like :

            public static IEnumerable<MCircle> ToSimple2DCircles(this IEnumerable<MCircle> circles, List<MLayer> workingLayers, int id)
            {
                return circles
                    .SelectMany(c => workingLayers, (c, wl) => new {c, wl})
                    .Where(t => t.c.Layer.Name == t.wl.LayerName)
                    .Select((x, i) => new MCircle(id + i, x.c.Radius, x.wl.LayerCategory));
            }


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me

    • Proposed as answer by simonb549 Thursday, February 27, 2020 7:51 AM
    • Marked as answer by essamce Monday, March 2, 2020 6:16 PM
    Wednesday, February 26, 2020 9:12 PM

All replies

  • Hi,

    Sorry I did not have time to test or optimize it, but try something like :

            public static IEnumerable<MCircle> ToSimple2DCircles(this IEnumerable<MCircle> circles, List<MLayer> workingLayers, int id)
            {
                return circles
                    .SelectMany(c => workingLayers, (c, wl) => new {c, wl})
                    .Where(t => t.c.Layer.Name == t.wl.LayerName)
                    .Select((x, i) => new MCircle(id + i, x.c.Radius, x.wl.LayerCategory));
            }


    Hope this helps,

    Here to learn and share. Please tell if an answer was helpful or not at all. This adds value to the answers and enables me to learn more.

    About me

    • Proposed as answer by simonb549 Thursday, February 27, 2020 7:51 AM
    • Marked as answer by essamce Monday, March 2, 2020 6:16 PM
    Wednesday, February 26, 2020 9:12 PM
  • Hi essamce,

    Thank you for posting here.

    KeesDijk's answer is correct, but you can also use the following code, they are actually the same.

     static IEnumerable<MCircle> ToSimple2DCircles(IEnumerable<MCircle> circles, List<MLayer> workingLayers, int id)
            {
                var collection = from c in circles
                                 from wl in workingLayers
                                 where c.Layer.Name == wl.LayerName
                                 select new MCircle(id++,c.Radius,wl.LayerCategory);
                return collection;
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 27, 2020 2:21 AM
  • Hi,

    Has your problem been solved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 2, 2020 9:19 AM
  • select new MCircle(++id,c.Radius,wl.LayerCategory
    Monday, March 2, 2020 10:52 AM