1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Fluent API question

Fluent API question

7 posts, 0 answered
  1. Owain
    Owain avatar
    110 posts
    Registered:
    17 Jan 2012
    03 Oct 2012
    Link to this post
    Hi!
    I have inheritted a bit of code from someone and I am trying to make some changes to it. Just now the code is:
    var dataSourceHome = App.WorkWith().NewsItems().Publihed().Get().Take(6).OrderByDescending(i => i.PublicationDate).ToList()
                       .Join(App.WorkWith()
                       .Images().Publihed()
                       .Get().Where(i => i.Parent.Title == "NewsArticles"), item => item.Title.Value, image => image.Title.Value,
                                    (item, image) => new { NewsItem = item, NewsImage = image }).ToList();

    So just now I get the articles but they only disply if there is an image with the same title as the news article. How would I put in an if statement in to this? I know how to do it normally but I'm still picking up how to use the Fluent API.
    I would like to say something like,

    IF no image matches the item.Title.Value
    use a generic image or don't show any image at all.

    Thanks,

  2. Patrick Dunn
    Patrick Dunn avatar
    237 posts
    Registered:
    03 Nov 2014
    04 Oct 2012
    Link to this post
    Hello,

     The fluent API uses traditional lamda expressions. You learn more about comparing values in lamda here. Take a look at one potential solution I have outlined below for you.

    var dataSourceHome = App.WorkWith().NewsItems().Publihed().Get().Take(6).OrderByDescending(i => i.PublicationDate).ToList()
                       .Join(App.WorkWith()
                       .Images().Publihed()
                       .Get().Where(i => i.Parent.Title == "NewsArticles"), item => item.Title.Value,
                       image => {
                           if (image.Title.Value != null)
                               return image.Title.Value;
                           else
                               return null;
                       },(item, image) => new { NewsItem = item, NewsImage = image }).ToList();

    Where it returns null you could specify a default image, etc. Let me know if you need any clarification! Regards,
    Patrick Dunn
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Owain
    Owain avatar
    110 posts
    Registered:
    17 Jan 2012
    05 Oct 2012
    Link to this post
    Hi Patrick, thanks for the reply. Very helpful.

    I had hoped this would solve my issue and in some ways it has, but for some other reason, Im only pulling back 2 news items and they aren't the latest published, to be honest, Im not sure why its pulling out only these two items, other than maybe they are the only 2 that have a matching image title, but as I say, I had hoped the if statement would sort that.
  4. Owain
    Owain avatar
    110 posts
    Registered:
    17 Jan 2012
    08 Oct 2012
    Link to this post
    Can anyone suggest why I am only pulling back 2 published items when I should be pulling back 6. I want to pull back the 6 latest news items and if they have a matching image (title name) then display that image, if they don't then still display the news but with no image.

  5. Patrick Dunn
    Patrick Dunn avatar
    237 posts
    Registered:
    03 Nov 2014
    10 Oct 2012
    Link to this post
    Hi Owain,

    Can you verify this part of your code:

    .Get().Where(i => i.Parent.Title == "NewsArticles")

    That the "NewsArticles" title is being used on the parent of the images you wish to grab?


    Greetings,
    Patrick Dunn
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. Owain
    Owain avatar
    110 posts
    Registered:
    17 Jan 2012
    12 Oct 2012 in reply to Patrick Dunn
    Link to this post
    Hi,
    Yes, the image library is called NewsArticles. It is pulling out the images if the image names are matching the news article name.
    I have decided just to use the default rotator included with Sitefinity and will look at implementing images at a later stage but would be interested to hear your thoughts on how to implement something.
    I've started looking at the SDK rotator as well, but it would be good to have an option to display a news article even if it doesnt have an image.
  7. Patrick Dunn
    Patrick Dunn avatar
    237 posts
    Registered:
    03 Nov 2014
    16 Oct 2012
    Link to this post
    Hello,

     Sorry to hear you're still having troubles. I would suggest you take a look at Slavo's blog post about associating an image rotator with a news item. He goes over how to set up either a single picture or an entire rotator for a news item. At the bottom he has a source code example  you can use.

    Kind regards,
    Patrick Dunn
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
7 posts, 0 answered