I have reproduced my problem in a console app. Following is the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LinqSample
{
class Program
{
static void Main(string[] args)
{
string query = "Maine, Maryland, Massachusetts, M";
var queries = query.Trim().Split((new[] { "," }), StringSplitOptions.RemoveEmptyEntries);
string lastQuery = queries[queries.Length - 1].Trim();
if (queries.Length > 1)
{
var remQueries = queries.Take(queries.Length - 1).ToArray();
string[] suggestions = new string[] {"Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota",
"Mississippi", "Missouri", "Montana"};
System.Console.WriteLine("Before Except ");
suggestions.ToList().ForEach(str => { System.Console.WriteLine(str); });
var unqiueSugs = from sug in suggestions
where !remQueries.Any(q => q.Equals(sug, StringComparison.OrdinalIgnoreCase))
select sug;
if (unqiueSugs != null)
{
suggestions = unqiueSugs.ToArray();
}
System.Console.WriteLine("\n After Except ");
suggestions.ToList().ForEach(str => { System.Console.WriteLine(str); });
}
}
}
}
After the above code, I except suggestions to contain all values except "Maine", "Maryland", "Massachusetts". But it is having 7 values that includes "Maryland", "Massachusetts". Where am I doing wrong? Kindly help.
EDIT: I am using dev studio 2008 with .NET 3.5 sp1. I am using above code in my action method of asp.net mvc