开发者

When searching through a sharepoint list how do I make sure it doesn't filter on capital?

My code so far:

List<SearchEntity> results = new List<SearchEntity>();
var title = tbTitle.Text;
var adress = tbAdress.Text;
var city = tbCity.Text;

foreach (var item in list.Items.Cast<SPListItem>().Where(r => title.Contains(r.Title) || adress.Contains((string)r["Adress"]) || city.Contains((string)r["City"])))
{
    var result = new SearchEntity
                     {
                         title = item.Title,
                         adress = (string)ite开发者_Go百科m["Adress"],
                         city = (string)item["City"],
                     };
    results.Add(result);
}

return results;

I have tried casting all the values .ToLower() and .ToUpper() but that didn't seem to do the trick.


Method String.Contains performs case-sensitive and culture-insensitive comparison, so if you want case-insensitive contain use IndexOf method like

title.IndexOf(r.Title, StringComparison.InvariantCultureIgnoreCase) >= 0

but in your code you fetch all items and filter record there, if List will contain many items, this way is not recommended, as it involves getting all items from list. Maybe it’s better use CAML Query (or LINQ in Sharepoint2010). What do you want to find? Items where fields Title/City/Address contain value from vars title/city/address or you want find items where fields Title/City/Address value contained in vars title/city/address. In 1st case your CAML will be like:

List<SearchEntity> results = new List<SearchEntity>();
var title = tbTitle.Text;
var adress = tbAdress.Text;
var city = tbCity.Text;
var query = new SPQuery()
{
    Query = string.Format(
        @"
                    <Where>
                        <Or>
                            <Or>
                                <Contains>
                                    <FieldRef Name=""Title"" />
                                    <Value Type=""Text"">{0}</Value>
                                </Contains>
                                <Contains>
                                    <FieldRef Name=""Adress"" />
                                    <Value Type=""Text"">{1}</Value>
                                </Contains>
                            </Or>
                            <Contains>
                                <FieldRef Name=""City"" />
                                <Value Type=""Text"">{2}</Value>
                            </Contains>
                        </Or>
                    </Where>", title, adress, city)
};
var items = list.GetItems(query);
foreach (var item in items)
{
    var result = new SearchEntity
    {
        title = item.Title,
        adress = (string)item["Adress"],
        city = (string)item["City"],
    };
    results.Add(result);
}
return results;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜