I'm having a little trouble deciding the best way to refactor a method which contains LINQ queries which are very similar but not identical.
Consider a method which is something along these lines:
public SomeObject GetTheObject(IMyObject genericObject) {
Type t = genericObject.GetType();
SomeObject so = null;
switch(t.Name) {
case "Type1":
var object1 = (from o in object1s where o.object1id == genericObject.id).FirstOrDefault();
so = (SomeObject)object1;
break;
case "Type2":
var object2 = (from o in object2s where o.object2id == genericObject.id).FirstOrDefault();
so = (SomeObject)object2;
break;
default:
break;
}
return so;
}
This is just an illustration, but imagine I'm needing to execute a different query (different in that it uses a different ObjectSet, uses slightly different fields (object1id vs object2id) and returns a different type. Other than that, the queries are the same.
Is there a sensible way to refactor this kind of method? It feels like I've missed something obvious. Perhaps I have to use the exact method and I can't avoid re-writing the query, it just seems like I SHOULD be able to somehow!
Any pointers greatly appreciated
LINQ queries are always executed when the query variable is iterated over, not when the query variable is created. This is called deferred execution. You can also force a query to execute immediately, which is useful for caching query results.
LINQ to SQL offers an infrastructure (run-time) for the management of relational data as objects. It is a component of version 3.5 of the . NET Framework and ably does the translation of language-integrated queries of the object model into SQL. These queries are then sent to the database for the purpose of execution.
LINQ queries can be written in two syntax types, a Query Syntax and a Method Syntax.
Maybe you have just oversimplified your scenario, but the smelly part of your function is the cast to SomeObject. Couldn't you just work with interfaces and (if needed) cast the result at the call site? You could have your Type1 and Type2 implement a common interface where id1 and id2 are exposed as id, for example (or decorate them if you don't control Type1 and Type2)
I.e.
public static IMyObject GetTheObject(List<IMyObject> theList, int id)
{
var ret = (from o in theList
where o.id==id
select o).FirstOrDefault();
return ret;
}
For instance, if you have:
public interface IMyObject {int id {get;}}
public class Foo : IMyObject {public int id {get; set;}}
public class Bar : IMyObject {public int id {get; set;}}
you can do:
var l1 = new List<IMyObject>(){new Foo(){id=1}, new Foo(){id=2}};
var l2 = new List<IMyObject>(){new Bar(){id=1}, new Bar(){id=2}};
var obj1 = Test.GetTheObject(l1, 1);
var obj2 = Test.GetTheObject(l2, 2);
And cast the objects after you call the function, if you have to.
EDIT: if you're stuck with concrete objects and casts, the best refactoring I could come up with is:
public static SomeObject GetTheObject(IMyObject genericObject) {
Type t = genericObject.GetType();
Func<SomeObject, bool> WhereClause = null;
IEnumerable<SomeObject> objs = null; // IEnumerable<T> is covariant,
// so we can assign it both an IEnumerable<object1>
// and an IEnumerable<object2> (provided object1 and 2 are
// subclasses of SomeObject)
switch(t.Name) {
case "Type1":
WhereClause = o => ((Object1)o).object1id == genericObject.id;
objs = object1s;
break;
case "Type2":
WhereClause = o => ((Object2)o).object2id == genericObject.id;
objs = object2s;
break;
}
var ob = objs
.Where(WhereClause)
.FirstOrDefault();
return (SomeObject)ob;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With