LINQ: Group by month and year within a datetime field

var grouped = from p in posts group p by new { month = p.Create.Month,year= p.Create.Year } into d select new { dt = string.Format(“{0}/{1}”,d.Key.month,d.Key.year), count = d.Count() }; Here’s the list of DateTime functions available in LINQ. For this to work you’ll also need to understand multi-column grouping ordered descending var grouped = (from … Read more

LINQ syntax where string value is not null or empty

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=367077 Problem Statement It’s possible to write LINQ to SQL that gets all rows that have either null or an empty string in a given field, but it’s not possible to use string.IsNullOrEmpty to do it, even though many other string methods map to LINQ to SQL. Proposed Solution Allow string.IsNullOrEmpty in a LINQ to … Read more

What instantiate-able types implementing IQueryable are available in .Net 4.0?

IQueryable objects are produced by Queryable Providers (ex. LINQ to SQL, LINQ to Entities/Entity Framework, etc). Virtually nothing you can instantiate with new in the basic .NET Framework implements IQueryable. IQueryable is an interface designed to be used to create Queryable providers, which allow the LINQ library to be leveraged against an external data store … Read more

What are the benefits of a Deferred Execution in LINQ?

The main benefit is that this allows filtering operations, the core of LINQ, to be much more efficient. (This is effectively your item #1). For example, take a LINQ query like this: var results = collection.Select(item => item.Foo).Where(foo => foo < 3).ToList(); With deferred execution, the above iterates your collection one time, and each time … Read more

Is there an Online version of LINQ Pad?

There are two online services that I know of, which don’t have the nice graphical features of LINQPad, but can be used as web-based C# (or VB.NET) scratch pads. I use them for testing the code in my StackOverflow answers on PCs without Visual Studio. Ideone http://ideone.com/ It uses mono 2.8 and, thus, supports LINQ … Read more

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

Moqing Entity Framework 6 .Include() using DbSet

For anyone who stumbles upon this issue with interest on how to solve the .Include(“Foo”) problem with NSubstitute and Entity Framework 6+, I was able to bypass my Include calls in the following way: var data = new List<Foo>() { /* Stub data */ }.AsQueryable(); var mockSet = Substitute.For<DbSet<Foo>, IQueryable<Foo>>(); ((IQueryable<Post>)mockSet).Provider.Returns(data.Provider); ((IQueryable<Post>)mockSet).Expression.Returns(data.Expression); ((IQueryable<Post>)mockSet).ElementType.Returns(data.ElementType); ((IQueryable<Post>)mockSet).GetEnumerator().Returns(data.GetEnumerator()); // … Read more

How are people unit testing code that uses Linq to SQL [closed]

Update: Fredrik has put an example solution on how to do unit test linq2sql applications over at his blog. You can download it at: http://web.archive.org/web/20120415022448/http://iridescence.no/post/DataContext-Repository-Pattern-Example-Code.aspx Not only do I think its great that he posted an example solution, he also managed to extract interfaces for all classes, which makes the design more decoupled. My old … Read more

Is LINQ to SQL deprecated?

For all the “Linq-to-SQL is dead” folks: Scott Guthrie himself clearly mentioned at TechEd Europe that Linq-to-SQL is FULLY SUPPORTED in .NET 4, and Damien Guard posted a blog post on what changes and improvements have been made for Linq-to-SQL in .NET 4. To quote Mark Twain: “Reports of my death have been greatly exaggerated”……

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)