What is default value of `KeyValuePair`? [duplicate]

The default value of any type T is default(T), so for 100% theoretical accuracy you would write if (!mapping.Equals(default(KeyValuePair<string, int>))) { // … } Since KeyValuePair is a struct (i.e. a value type) you cannot compare it to null. Irrespective of that, comparing values with == is wrong as a default approach because if used … Read more

Multiple where conditions in EF [duplicate]

You can chain your where clauses. You just need an IQueryable datasource. var filteredData = _repository.GetAll(); //If your data source is IEnumerable, just add .AsQueryable() to make it IQueryable if(keyWordTextBox.Text!=””) filteredData=filteredData.Where(m=>m.Keyword.Contains(keyWordTextBox.Text)); if(LocationDropDown.SelectedValue!=”All”) filteredData=filteredData.Where(m=>m.Location==LocationDropDown.SelectedValue)); … etc…. Because it is IQueryable, the data is not fetched until you bind it so it only pulls the data you … Read more

DISTINCT() and ORDERBY issue

From the Queryable.Distinct documentation; The expected behavior is that it returns an unordered sequence of the unique items in source. In other words, any order the existing IQueryable has is lost when you use Distinct() on it. What you want is probably something more like this, an OrderBy() after the Distinct() is done; var query … Read more

Why LINQ to Entities does not recognize the method ‘System.String ToString()?

That can’t be converted to SQL. I guess, in theory, it could, but isn’t implemented. You just need to perform your projection after you have your results: var pricingSecurityID = (from m in dbContext.Reporting_DailyNAV_Pricing select m.PricingSecurityID).AsEnumerable() .Select(x => new SelectListItem{ Text = x.ToString(), Value = x.ToString() });