Convert datetime to a formatted string inside a LINQ-to-entities query

Another option is using SqlFunctions.DateName, your code will be like this: var offer = (from p in dc.CustomerOffer join q in dc.OffersInBranch on p.ID equals q.OfferID where q.BranchID == singleLoc.LocationID let value = (p.OriginalPrice – p.NewPrice) * 100 / p.OriginalPrice orderby value descending select new { Title = p.OfferTitle, Description = p.Description, BestOffer = value, … Read more

LINQ Where Ignore Accentuation and Case

To ignore case and accents (diacritics) you can first define an extension method like this: public static string RemoveDiacritics(this String s) { String normalizedString = s.Normalize(NormalizationForm.FormD); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < normalizedString.Length; i++) { Char c = normalizedString[i]; if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark) stringBuilder.Append(c); } return stringBuilder.ToString(); } (Modified … Read more

Nested query in entity framework

You’re overestimating the power of LINQ translation to SQL. Not everything is translatable and there is no compiler warning for that due to the way LINQ works. Nested collections are usually either a) not supported or b) end up in horrible SELECT N+1 queries. What you ask EF to do is to return an object … Read more

Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?

To understand the “display class” you have to understand closures. The lambda you pass here is a closure, a special type of method that magically drags in state from the scope of the method it’s in and “closes around” it. …except of course that there’s no such thing as magic. All that state has to … Read more

how to use entity framework to group by date not date with time

Use EntityFunctions.TruncateTime Method (Nullable<DateTime>). It will be transalated into TRUNCATETIME() TSQL function in generated SQL query, which does what you need: Returns the expression, with the time values truncated. So your code should be as follows: //get data var myData = from log in db.OperationLogs group log by EntityFunctions.TruncateTime(log.CreateTime) into g orderby g.Key select new … Read more