Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6912535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:06:15+00:00 2026-05-27T09:06:15+00:00

I’m having a little trouble deciding the best way to refactor a method which

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:06:16+00:00Added an answer on May 27, 2026 at 9:06 am

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.