refactor-my-code

Tricky ActiveRecord + SQL Query (Refactor)

Here's my current named_scope. It returns an aggregate result from a number of rows. Sample result from 300 rows: Total Points: 3000 Wins: 31 Losses: 11 Here is the code: named_scope :open_for, lambda { |sportable_id, sportable_type, days_ago| { :conditions => ['sportable_id = ? and sportable_type = ? and game_time > ? ', sport...

jQuery delegate optimization

what is the best way to optimize the following snippets that uses delegate()? jQuery('.menu').delegate('li.gallery', 'hover', function () { jQuery(this).children('.submenu').toggleClass('hide show'); }); jQuery('.menu').delegate('li.interior', 'hover', function () { jQuery(this).children('.submenu').toggleClass('hide show'); }); jQuery(...

C# Function that accepts string parameters: How to refactor to use something strongly typed?

I have this C# code that works just fine, which grabs any two fields and puts them in a list for a Drop Down List: var myDDL = GetDDLValues<Product>( myContact, "contactid", "companyname"); I would like it to take the two string parameters in something other than strings. This would be really nice to do: GetDDLValues<Product>( myCon...

What C#/Linq code copies all matching property name values between two objects without knowing their type?

I have a well known POCO class of Customer to return from my method. However, I only populate properties specified by an ever changing Expression p => new {p.id, p.name} for example, as a parameter to the method. Somehow I need to copy all matching fields between these two objects. var returnObject = IList<Customer>(); var partialField...

Given a Generic <E> how to create a new E and return it?

This code returns a variable set of fields and I want to return a strongly typed <E>: public IList<E> Get(Expression<Func<E, object>> selectLambda == null) { if (selectLambda == null) selectLambda = p => p; var partialSet = DC.CreateQuery<E>("[" + typeof(E).Name + "]"); foreach ( var record in partialSet) { ...

Shorter way to consume a WCF async method in ASP.NET

Hello is there any shorter way to consume an asynchronous WCF method in a ASP.NET page? here is a sample I would like to refactor: protected void Button1_Click(object sender, EventArgs e) { PageAsyncTask c = new PageAsyncTask(AsyncList, CallbackList, null, null); Page.RegisterAsyncTask(c); } IAsyncResult AsyncList(object s...

c# List<string> to Lambda Expression with starter example: Refactor to handle the List

I have this: List<string> fields; fields[0] = "firstFieldName"; fields[1] = "secondFieldName"; ... fields[n] = "nthFieldName"; I want to get this: var selector = p => new {p.firstField, p.secondField, ..., p.nthFieldName} // selector is of type Expression<Func<Entity, object>> GoofBallLogic had this code that was simliar, ending ...

Turn "fieldname .startsWith. 'fieldValue'" into .Where(p => p.fieldname.StartsWith('fieldValue')) predicate?

I attempted to do this below, with mind blanks as it got complicated. I need to build a predicate to pass to my Product Repository from a client string: "fieldname .startsWith. 'fieldvalue'" "fieldname .contains. 'fieldvalue'" "fieldname .equals. 3" "fieldname .greaterThan. 3" "fieldname .lessThan. 3" The field names could be any exi...