Search code examples
c#linqsharepointlinq-to-sharepoint

matching string to item ids in LINQ


I have a string:

strCheckedCategories = "2;"

an EntityList representing a SharePoint list, with item IDs from 1 to 21:

EntityList<VendorSearchesItem> vendorSearches = 
    dataContext.GetList<VendorSearchesItem>("Vendor Searches");

a LINQ query returning fields from two SharePoint lists that are joined to the "Vendor Searches" list:

var vendorSearchesQuery = (from s in vendorSearches
                           orderby s.VendorID.Title
                           select new
                           {
                               Vendor = s.VendorID.Title,
                               Website = s.VendorID.VendorWebsite,
                               VendorID = s.VendorID.Id,
                               SearchType = s.SearchTypeID.Title,
                               SearchTypeId = s.SearchTypeID.Id
                           });

and another LINQ query returning only the items where the item ID is in the list:

var q2 = from m2 in vendorSearchesQuery 
         where strCheckedCategories.Contains(m2.SearchTypeId.ToString())
         select m2

The problem is that, in addition to returning the item with ID 2 (desired result) the query also returns items with ID 12, 20, and 21. How can I fix that?


Solution

  • try:

    strCheckedCategories.Split(new []{';'}).Any(x => x == m2.SearchTypeId.ToString())
    

    Contains will do a substring match. And "20" has a substring "2".