Fluent API question

Posted by Community Admin on 04-Aug-2018 10:11

Fluent API question

All Replies

Posted by Community Admin on 03-Oct-2012 00:00

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,

Posted by Community Admin on 04-Oct-2012 00:00

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

Posted by Community Admin on 05-Oct-2012 00:00

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.

Posted by Community Admin on 08-Oct-2012 00:00

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.

Posted by Community Admin on 10-Oct-2012 00:00

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

Posted by Community Admin on 12-Oct-2012 00:00

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.

Posted by Community Admin on 16-Oct-2012 00:00

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

This thread is closed