1

Here I have a list of string in sites1. I need to check the common items between sites1 and items1 and select the matching items from items1. Here is my code

 string query = "/sitecore/content/*";
 List<string> sites1 = Sitecore.Configuration.Settings.Sites.Select(x => x.StartItem.TrimStart('/')).ToList();
 List<Item> items1 = Sitecore.Context.Database.SelectItems(query).Where(x => x.DisplayName.Contains(sites1)).ToList();

Any suggestion?

Edit: Here i am getting the error while selecting two items

 var sites = Sitecore.Configuration.Settings.Sites.Select(f => new List<string>() { f.StartItem.TrimStart('/'), f.Language }).ToList();
 List<Item> items = Sitecore.Context.Database.SelectItems(query).Where(x => sites.Contains(x.DisplayName.ToLower())).ToList();
5
  • 1
    Tried it and getting the following error 'Sitecore.Data.Items.Item' does not contain a definition for 'sites1' and no extension method 'sites1' accepting a first argument of type 'Sitecore.Data.Items.Item' could be found (are you missing a using directive or an assembly reference?) Commented Jan 9, 2016 at 6:50
  • 1
    Sorry mad bad! sites1.Contains(x.DisplayName) remove x. Commented Jan 9, 2016 at 6:51
  • @Rooney: Where do you use "Language" field ? Commented Jan 9, 2016 at 8:37
  • @alexm: I need it for later usage Commented Jan 9, 2016 at 8:39
  • @Rooney: Do you use "Language" in the second query SelectItems().Where(...) ? Commented Jan 9, 2016 at 8:42

2 Answers 2

2

It should be the other way around: site1.Contains(x.DisplayName). Also, compared to a list HashSet<string> is more efficient for multiple look-ups, which becomes noticable as the number of items increases.

var sites1 = new HashSet<string>(Sitecore.Configuration.Settings.Sites
        .Select(x => x.StartItem.TrimStart('/')));

var items1 = Sitecore.Context.Database.SelectItems(query)
        .Where(x => site1.Contains(x.DisplayName))
        .ToList();

EDIT:

I did not notice that SelectItems() returns IQueriable. In that case I would re-write the second statement using IEnamerable.Any<> extention method, which can be projected into server SQL query.

var items1 = Sitecore.Context.Database.SelectItems(query)
        .Where(x=>sites1.Any(it=>it == x.DisplayName))
        .ToList();

EDIT 2: Correcting the query from the edited question:

var sites1 = Sitecore.Configuration.Settings.Sites
        .Select(x => new {DisplayName = x.StartItem.TrimStart('/'), Language = x.Language});

var siteNames = new HashSet<string>(sites1.Select(x=> x.DisplayName.ToLower());

var items1 = Sitecore.Context.Database.SelectItems(query)
        .Where(x=>siteNames.Any(it=>it == x.DisplayName.ToLower()))
        .ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

DisplayName doesnot exist
Working fine now..Appreciate your help
2
List<Item> items1 = Sitecore.Context.Database.SelectItems(query).Where(x => sites1.Contains(x.DisplayName)).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.