issue using indexOf function in fluent API query
hello,
I am trying to get all content items that have a specific tag but I get exception "Invalid method" regarding the use of the indexOf method. Below is my code. How should I go about accomplishing this task?
this.KBList.DataSource = App.WorkWith().ContentItems().Where(n => n.Status == ContentLifecycleStatus.Live && n.TagsText.IndexOf("KB") > -1).Get().Take(this.kbLimit).ToList();
Hello Dan,
our implementation of LINQ does not support IndexOf method. You can use Contains as an alternative.
this.KBList.DataSource = App.WorkWith().ContentItems().Where(n => n.Status == ContentLifecycleStatus.Live && n.TagsText.Contains("KB")).Get().Take(this.kbLimit).ToList();
hi Ivan,
Thanks for the reply. I am now getting the following exception when using the code you supplied:
"line 1:156: Field \"TagsText\" not found in Class Telerik.Sitefinity.GenericContent.Model.ContentItem. TagsText [\"TagsText\",<42>,line=1,col=156]\r\nOriginal Query: DEFINE EXTENT extnt FOR Telerik.Sitefinity.GenericContent.Model.ContentItem; SELECT * FROM extnt AS t1 WHERE t1.appName = $1 AND (t1.status = $2 AND t1.TagsText LIKE $3 = $4 )"
I am using the latest build (4.0.1130.0).
thanks
Dan
Hi Dan,
The error says that "TagsText" is not found for ContentItem. Do you have this field. Can you paste the code you use?
Greetings,
Ivan Dimitrov
the Telerik team
hi Ivan,
Below is my code:
protected override void InitializeControls(GenericContainer controlContainer)
var contentManager = ContentManager.GetManager();
var allContent = contentManager.GetContent();
this.KBList.DataSource = App.WorkWith().ContentItems().Where(n => n.Status == ContentLifecycleStatus.Live && n.TagsText.Contains(" KB ") == true).Get().Take(this.kbLimit).ToList();
Hello Dan,
Ok, I see what the problem is. Actually you have to use the ID of the item
var taxID= new Guid("61FF245B-23C7-453F-8DE6-2FF8EC46E125");
var items = App.WorkWith()
.ContentItems
()
.Where(n => ((IList<Guid>)n.GetValue("HereFieldNameYouWantToGet")).Contains(taxID))
.Get();
Regards,
Ivan Dimitrov
the Telerik team
hi Ivan,
ContentItem "n" does not have a method called GetValue. Should I be using another method instead?
thanks
Dan
Hi Dan,
Please make sure that you have reference to
Telerik.Sitefinity.Model.
GetValue is part of Telerik.Sitefinity.Model.DataExtensions
All the best,
Ivan Dimitrov
the Telerik team
Hi Ivan,
ContentManager manager = ContentManager.GetManager();
IQueryable<
ContentItem
> allItems = manager.GetContent()
.Where(n => n.Status == ContentLifecycleStatus.Live);
List<
ContentItem
> itemList = new List<
ContentItem
>(1);
foreach (ContentItem item in allItems)
if (item.TagsText.IndexOf(" KB ") > -1)
itemList.Add(item);
this.KBList.DataSource = itemList;
Hello Dan,
taxID is the name of the category or tag you want to filter by.
Greetings,
Ivan Dimitrov
the Telerik team