Use LINQ to get items in one List, that are not in another List

This can be addressed using the following LINQ expression: var result = peopleList2.Where(p => !peopleList1.Any(p2 => p2.ID == p.ID)); An alternate way of expressing this via LINQ, which some developers find more readable: var result = peopleList2.Where(p => peopleList1.All(p2 => p2.ID != p.ID)); Warning: As noted in the comments, these approaches mandate an O(n*m) operation. … Read more

‘Contains()’ workaround using Linq to Entities?

Update: EF ≥ 4 supports Contains directly (Checkout Any), so you don’t need any workaround. public static IQueryable<TEntity> WhereIn<TEntity, TValue> ( this ObjectQuery<TEntity> query, Expression<Func<TEntity, TValue>> selector, IEnumerable<TValue> collection ) { if (selector == null) throw new ArgumentNullException(“selector”); if (collection == null) throw new ArgumentNullException(“collection”); if (!collection.Any()) return query.Where(t => false); ParameterExpression p = selector.Parameters.Single(); … Read more

Why Response.Redirect causes System.Threading.ThreadAbortException?

The correct pattern is to call the Redirect overload with endResponse=false and make a call to tell the IIS pipeline that it should advance directly to the EndRequest stage once you return control: Response.Redirect(url, false); Context.ApplicationInstance.CompleteRequest(); This blog post from Thomas Marquardt provides additional details, including how to handle the special case of redirecting inside … Read more

How can I convert an integer into its verbal representation?

Currently the best, most robust, library for this is definitely Humanizer. It’s open sourced and available as a nuget: Console.WriteLine(4567788.ToWords()); // => four million five hundred and sixty-seven thousand seven hundred and eighty-eight It also has a wide range of tools solving the small problems every application has with strings, enums, DateTimes, TimeSpans and so … Read more

Creating a DateTime in a specific Time Zone in c#

Jon’s answer talks about TimeZone, but I’d suggest using TimeZoneInfo instead. Personally I like keeping things in UTC where possible (at least for the past; storing UTC for the future has potential issues), so I’d suggest a structure like this: public struct DateTimeWithZone { private readonly DateTime utcDateTime; private readonly TimeZoneInfo timeZone; public DateTimeWithZone(DateTime dateTime, … Read more

WCF – How to Increase Message Size Quota

You’ll want something like this to increase the message size quotas, in the App.config or Web.config file: <bindings> <basicHttpBinding> <binding name=”basicHttp” allowCookies=”true” maxReceivedMessageSize=”20000000″ maxBufferSize=”20000000″ maxBufferPoolSize=”20000000″> <readerQuotas maxDepth=”32″ maxArrayLength=”200000000″ maxStringContentLength=”200000000″/> </binding> </basicHttpBinding> </bindings> And use the binding name in your endpoint configuration e.g. … bindingConfiguration=”basicHttp” … The justification for the values is simple, they are sufficiently … Read more

Will the IE9 WebBrowser Control Support all of IE9’s features, including SVG?

WebBrowser control will use whatever version of IE you have installed, but for compatibility reasons it will render pages in IE7 Standards mode by default. If you want to take advantage of new IE9 features, you should add the meta tag <meta http-equiv=”X-UA-Compatible” content=”IE=9″ > inside the <head> tag of your HTML page. This meta … Read more

What is the difference between IQueryable and IEnumerable?

First of all, IQueryable<T> extends the IEnumerable<T> interface, so anything you can do with a “plain” IEnumerable<T>, you can also do with an IQueryable<T>. IEnumerable<T> just has a GetEnumerator() method that returns an Enumerator<T> for which you can call its MoveNext() method to iterate through a sequence of T. What IQueryable<T> has that IEnumerable<T> doesn’t … Read more

Which method performs better: .Any() vs .Count() > 0?

If you are starting with something that has a .Length or .Count (such as ICollection<T>, IList<T>, List<T>, etc) – then this will be the fastest option, since it doesn’t need to go through the GetEnumerator()/MoveNext()/Dispose() sequence required by Any() to check for a non-empty IEnumerable<T> sequence. For just IEnumerable<T>, then Any() will generally be quicker, … Read more