none
Using LINQ select sublist object from object list by objectId and sublist objectId RRS feed

  • Question

  • I have the follow code but I think there is an easier way to do it via LINQ.  Could anyone help?

     public string GetCssTheme(int applicationId, int themeId) {
                ApplicationCss appCss = (ApplicationCss) AppCss.Where(p => p.ApplicationId == applicationId);
                ThemeCss themeCss = (ThemeCss) appCss.Themes.Where(a => a.ThemeID == themeId);
                return themeCss.Css;
            }


    Tuesday, January 8, 2019 2:39 PM

Answers

  • Not sure I follow what you mean by easier and LINQ. You're using LINQ. LINQ is the set of extensions available on IEnumerable<T> to allow you to use SQL-style calls to query and manipulate data. That is what you're doing. Do you mean LINQ syntax? If so then I'd ask why. Sometimes using the extension methods are cleaner than LINQ.

    Also, it doesn't seem like your code should compile at all. Almost all LINQ methods return IEnumerable<T>, including Where. So themeCss is IEnumerable<ThemeCss>. There wouldn't be a Css property on that. It seems like you want the first (or only) one.

    public string GetCssTheme ( int applicationId, int themeId )
    {
       //Could combine these but why - makes debugging harder?
       var app = AppCss.FirstOrDefault(a => a.ApplicationId == applicationId);
                
       //Using null conditional (?.) here, if you're using an older version of VS then use a conditional instead
       return app?.Themes.FirstOrDefault(t => t.ThemeID == themeId)?.Css;
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 8, 2019 2:55 PM
    Moderator

All replies

  • Not sure I follow what you mean by easier and LINQ. You're using LINQ. LINQ is the set of extensions available on IEnumerable<T> to allow you to use SQL-style calls to query and manipulate data. That is what you're doing. Do you mean LINQ syntax? If so then I'd ask why. Sometimes using the extension methods are cleaner than LINQ.

    Also, it doesn't seem like your code should compile at all. Almost all LINQ methods return IEnumerable<T>, including Where. So themeCss is IEnumerable<ThemeCss>. There wouldn't be a Css property on that. It seems like you want the first (or only) one.

    public string GetCssTheme ( int applicationId, int themeId )
    {
       //Could combine these but why - makes debugging harder?
       var app = AppCss.FirstOrDefault(a => a.ApplicationId == applicationId);
                
       //Using null conditional (?.) here, if you're using an older version of VS then use a conditional instead
       return app?.Themes.FirstOrDefault(t => t.ThemeID == themeId)?.Css;
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, January 8, 2019 2:55 PM
    Moderator
  • Hi Matteus_Beus,

    Is there any update? if the issue still exists, please feel free let us know.

    Best regards,

    Zhanglong


    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, January 10, 2019 1:25 AM
    Moderator
  • I see so keep it this way as it's easier to debug! :)
    Tuesday, February 5, 2019 10:55 AM