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

  • Home
  • SEARCH
  • 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 8597823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:00:14+00:00 2026-06-12T01:00:14+00:00

I have the following DBML modification (i’m using Linq to SQL as a DAL).

  • 0

I have the following DBML modification (i’m using Linq to SQL as a DAL).

public interface ILinqSQLObject { }

// these are objects from SQL Server mapped into Linq to SQL
public partial class NEWDEBT : ILinqSQLObject { }
public partial class OLDDEBT : ILinqSQLObject { }
public partial class VIPDEBT : ILinqSQLObject { }

With that i can manipulate my Linq objects more properly on other areas.

I’ve just done an IRepository pattern implementation.

public interface IDebtManager<T>
    {
        IQueryable<T> GetAllDebts();
        IQueryable T GetSpecificDebt(System.Linq.Expressions.Expression<Func<T, bool>> predicate);
        void Insert(T debt);
        // other methods
    }

public class DebtManager<T> : IDebtManager<T> where T : class, ILinqSQLObject 
    {
        DebtContext conn = new DebtContext();
        protected System.Data.Linq.Table<T> table;

        public DebtManager()
        {
            table = conn.GetTable<T>();
        }

        public void Insert(T debt)
        {
            throw new NotImplementedException();
        }

        public IQueryable<T> GetSpecificDebt(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
        {
            return table.Where(predicate);
        }

        public IQueryable<T> GetAllDebts()
        {
            return table;
        }
    }

And that works flawlessly. But, there are sometimes that i don’t know, on compilation time, which specific table i’ll be using. For that i tried to create a simple generic factory for my DebtManager.

public static class DebtFactoryManager
{

    public static DebtManager<ILinqSQLObject> GetDebtManager(string debtType) 
    {
        switch (debtType)
        {
            case "New Client":
                return new DebtManager<NEWDEBT>();
            case "Old Client":
                return new DebtManager<OLDDEBT>();
            case "VIP Client":
                return new DebtManager<VIPDEBT>();
            default:
                return new DebtManager<NEWDEBT>();
        }

        return null;
    }

}

However it doesn’t work. It says that i cannot ‘implicity convert DebtManager<NEWDEBT> to DebtManager<ILinqSQLObject>‘, but if NEWDEBT implements ILinqSQLObject, why isn’t the compiler recognizing it? Obviously i’m doing some mistake but i can’t see it.

  • 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-06-12T01:00:17+00:00Added an answer on June 12, 2026 at 1:00 am

    This error is caused by the fact that generics do not implicitly support covariance; that is, treating a specific generic parameter type as if it were one of its base types.

    Couple ways around this. First, you can define a non-generic DebtManager base class that the generic DebtManager inherits from, and return that. Second, you can define a generic interface that DebtManager implements; generic interfaces CAN be defined to be covariant, by using the keyword out before the generic type parameter.

    EDIT: Let’s go back to the primary need. You may not know, at compile-time, what type of object you will be required to work with and therefore you don’t know which Repository you need. Might I suggest, then, that instead of a Repository-per-table architecture, you use a Repository-per-database. DebtManager is already generic to any Linq to SQL type; why not then make the methods generic as well, allowing them to be generic from call to call?

    public interface IRepository<T> where T:class, ILinqSqlObject
    {
        IQueryable<TSpec> GetAllDebts<TSpec>() where TSpec : T;
        IQueryable<TSpec> GetSpecificDebt<TSpec>(System.Linq.Expressions.Expression<Func<TSpec, bool>> predicate) where TSpec : T;
        void Insert<TSpec>(TSpec debt) where TSpec:T;
        // other methods
    }
    
    interface IDebtObject : ILinqSqlObject
    
    public interface IDebtManager:IRepository<IDebtObject> { }
    
    public class DebtManager:IDebtManager
    {
        DebtContext conn = new DebtContext();
    
        public DebtManager()
        {            
        }
    
        public void Insert<T>(T debt) where T:IDebtObject
        {
            throw new NotImplementedException();
        }
    
        public IQueryable<T> GetSpecificDebt(System.Linq.Expressions.Expression<Func<T, bool>> predicate) where T:IDebtObject
        {
            return conn.GetTable<T>().Where(predicate);
        }
    
        public IQueryable<T> GetAllDebts<T>() where T:IDebtObject
        {
            return conn.GetTable<T>();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following nested objects. I am using @Valid for validation in my controller.
I have following SQL statementL: select DATE(bla), count(*) from tableA group by DATE(bla) UNION
Have following code: public interface ITest { string St1 { get; } } public
I have an ASP.NET MVC 3 Web Application using Linq-to-SQL for my data access
I have following design requirements: interface Server {} public class ServerImpl implements Server {}
I have the following 3 classes in my dbml file: public class Player {
Have following String built SQL query: StringBuilder querySelect = new StringBuilder(select * from messages
I have following plist: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE plist PUBLIC -//Apple//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd>
Assume I have a table called User . Using LINQ desinger, I will end
I am trying to teach myself LINQ to SQL and have decided to do

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.