Fluent API question
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();
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();
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.
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.
Hi Owain,
Can you verify this part of your code:
.Get().Where(i => i.Parent.Title == "NewsArticles")
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.
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,