LINQ
There are 7 entries for the tag LINQ
Wednesday, August 18, 2010 11:58 AM | Comments

A year ago I had written briefly about my love-affair with the yield keyword. Today a question was posed to me and the follow-up discussion was interesting enough to me to warrant a quick post. If you're returning an IEnumberable<T> from a method that reads from a DataReader, is it bad to use yield? My concern is calling a long-running method while looping over the items returned from the method – won’t this keep the SQL connection open longer? Also what if an exception happens while looping, will my reader still get closed? His proposed API was similar to the following very common (and outdated, IMHO) pattern – which is turning a DataReader into data access or business objects. The Proposed API public List<string> GetItemsList() { using (var reader = new FakeDataReader()) { ...

Thursday, July 22, 2010 10:35 PM | Comments

A common requirement for many applications is filtering rows of data based on the current user. A simple example is an e-commerce site which would want to make sure a Customer who is logged in is only able to view his own Orders. Another example would be a payroll site that would want to make sure a user could only view their own Employees. These are of course trivial examples, and could be solved in a number of ways. My concern is that often times the responsibility of security is placed upon each individual developer to make sure they are calling the right methods on your repositories. This may seem fine to many readers, and we could debate whether or not this should be the case, but my current take on security is to let the application infrastructure handle those details for you. What if I could easily...

Monday, September 7, 2009 9:55 PM | Comments

This was one of the first extension methods I wrote when C# 3.0 arrived, since I just stumbled across it on an older project I figured I might as well post it, if for nothing else but future reference. One downside to be aware of if you choose to use this method, is that it will not project the results on a IQueryable provider like LINQ to SQL, so please be aware the conversion will be done on the client side. The Code public static class DelimitedStringExtensions { public static string DefaultDelimiter = ", "; /// <summary> /// Convert a sequence of items to a delimited string. By default, ToString() will be called on each item in the sequence to formulate the result. The default delimiter of ', ' will be used /// </summary> public static string...

Friday, September 4, 2009 2:14 PM | Comments

LINQ and Expression Trees have enabled a ton of possibilities for us as developers, particularly in establishing some code-reducing convention based querying. I have found myself playing with Expressions in C# since their inception and will hopefully be publishing a few other posts I have queued up. For now, this post will simply demonstrate how to take advantage of a popular convention whereby each database table always has a single integer primary key named “Id” By taking advantage of this primary key convention I can save myself a ton of boilerplate code to query a table by ID. The Extension Method public static T GetById<T>(this IQueryable<T> query, int id) where T : class { // Create lambda parameter x of type T var param = Expression.Parameter(typeof(T), "x"); // Given x param, extract the Id property var...

Monday, January 12, 2009 6:50 AM | Comments

A couple months ago I wrote a basic set of extension methods to handle automatic auditing in LINQ to SQL. Well I have received a large number of emails regarding this particular project so I have decided to focus on cleaning up my v2 API and releasing it on CodePlex. There was a lot of room for improvement from version 1 and today I am going to post the all new LINQ Audit Trail code. This new version is significantly enhanced in the previous version. Objective Automatic auditing of all inserts/updates/deletes for any table in your database with a single line of code, including: What table was modified? What fields changed? Who made the change? When did it...

Thursday, May 8, 2008 6:10 AM | Comments

This post is out of date This post is outdated and has significantly enhanced with a new version. Please see LINQ Audit Trail V2 In a project I am currently working on, we had a fairly common requirement of recording an Audit Trail of any data changes. The requirements were typical, provide a running log of any changes in the database, including: What table was modified? What fields changed? Who made the change? When did it occur? Naturally, there are many ways to tackle this problem. In the past I have either relied on writing database Triggers for the tables, or wrote the Auditing logic right into the stored procedure that was doing the modification. But this time, since the project is utilizing...

Tuesday, February 26, 2008 5:59 AM | Comments

This post is a follow-up to an article I wrote a few weeks ago, ADO.NET Entity Framework Comparison Frustration. As a quick re-cap, I was simply trying to filter a list of users with a LINQ query expression by comparing custom classes, not primitive types. TorvusEntities entities = new TorvusEntities(); // Pull my Account Entity from the database Account matt = entities.Accounts.First(a => a.AccountId == 10); // Attempt to get all Teams by a Team Owner var teams = from t in entities.Teams where t.Owner == matt select t; However, an exception was thrown. Unable to create a constant value of type 'Closure type'. Only primitive types (for instance Int32, String and Guid) are supported in this context. So I left it at that....