Entity Framework, Code First and Full Text Search

Using interceptors introduced in EF6, you could mark the full text search in linq and then replace it in dbcommand as described in http://www.entityframework.info/Home/FullTextSearch: public class FtsInterceptor : IDbCommandInterceptor { private const string FullTextPrefix = “-FTSPREFIX-“; public static string Fts(string search) { return string.Format(“({0}{1})”, FullTextPrefix, search); } public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext) { } … Read more

The specified type member ‘Date’ is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties

DateTime.Date cannot be converted to SQL. Use EntityFunctions.TruncateTime method to get date part. var eventsCustom = eventCustomRepository .FindAllEventsCustomByUniqueStudentReference(userDevice.UniqueStudentReference) .Where(x => EntityFunctions.TruncateTime(x.DateTimeStart) == currentDate.Date); UPDATE: As @shankbond mentioned in comments, in Entity Framework 6 EntityFunctions is obsolete, and you should use DbFunctions class, which is shipped with Entity Framework.

writing a custom comparer for linq groupby

The grouping algorithm (and I think all LINQ methods) using an equality comparer always first compares hash codes and only executes Equals if two hash codes are equal. You can see that if you add tracing statements in the equality comparer: class PointComparer : IEqualityComparer<Point> { public bool Equals(Point a, Point b) { Console.WriteLine(“Equals: point … Read more

Expose IQueryable Over WCF Service

You should check WCF Data Services which will allow you to define Linq query on the client. WCF Data Services are probably the only solution for your requirement. IQueryable is still only interface and the functionality depends on the type implementing the interface. You can’t directly expose Linq-To-Sql or Linq-To-Entities queries. There are multiple reasons … Read more

LINQ To SQL exception: Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains operator

Replace the usages of Any with Contains in your query. eg: searchTerms.Contains(c.Email) This should get the result you’re looking for. It looks backwards, but it’s correct- it’ll generate an IN operator for each field inside a Contains with all the elements in searchTerms. The AddressLine1 part won’t work this way- you’ll have to loop-generate the … Read more

Linq To Entities – how to filter on child entities

There is no “nice” way of doing this, but you could try this – project both, Group and filtered Users onto an anonymous object, and then Select just the Groups: var resultObjectList = AllGroups. Select(g => new { GroupItem = g, UserItems = g.Users.Where(u => !u.IsInactive) }).ToList(); FilteredGroups = resultObjectList.Select(i => i.GroupItem).ToList(); This isn’t a … Read more