1

I'm working on a bilingual website, i.e. Persian (=Farsi) and English. I've created two sets of models, one for each object, and they inherit from one master object. For example, the Book class:

public class Book
{
    public int id { get; set; }
    public string title { get; set; }
    ...
}

public class Book_fa : Book {
    public PersianMonth? month { get; set; }
    ...
}

public class Book_en : Book { 
    public EnglishMonth? month { get; set; }
    ...
}

(both PersianMonth and EnglishMonth are enum)

And these objects are stored in two tables:

public class myDbContext : DbContext
{
    public myDbContext()
        : base("name=myDbName")
    {
    }

    public DbSet<Book_fa> Books_fa { get; set; }
    public DbSet<Book_en> Books_en { get; set; }
}

I can create a separate controller and views for each object. For example, the Delete action in Book_faController:

private myDbContext db = new myDbContext();

public ActionResult Delete(int id)
{
    Book_fa book_fa = db.Books_fa.Find(id);
    db.Books_fa.Remove(book_fa);
    db.SaveChanges();
    return RedirectToAction("Index");
}

But the Delete action in Book_enController is the same! Except the db.Books_fa term which must changed to db.Books_en. This is true for any other action (create, edit, listing, ....).

So i decided to use just one controller to deal with these bilingual books! I've tried this:

public ActionResult Anything(string lang, ...)
{
    var table = getTable(lang);
    ...
}

private DbSet getTable(lang)
{
    return (lang == "fa") ? db.Books_fa : db.Books_en;
}

But this is not working :(

Can anyone help me solve this?

4
  • 2
    Why it is not working? What problems are you having? Commented Mar 3, 2015 at 15:11
  • the methods like OrderBy ,ToList and ... are not available for table (return of getTable) Commented Mar 3, 2015 at 15:15
  • Tried returning ((lang == "fa") ? db.Books_fa : db.Books_en) as IEnumerable;? Commented Mar 3, 2015 at 15:20
  • yes i tried it now. the problem not solved. thanks anymore... Commented Mar 3, 2015 at 15:25

1 Answer 1

2

You could return an IDbSet<Book> from your GetTable method:

private IDbSet<Book> GetTable(string lang)
{
    return lang == "fa" ? (IDbSet<Book>)db.Books_fa : (IDbSet<Book>)db.Books_en;
}

Now you will have all the OrderBy, ToList, ... method at your disposal on the result of this method so that you can continue filtering the results without eagerly executing the SQL query yet, just building the expression tree.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot! solved! but now a new question: is there a way to choose desired table out of actions? consider this controller heve 20 actions, so i have to use GetTable 20 times. is it possible to reduce usage of GetTable?
What's the problem with calling GetTable multiple times? Don't forget that this method only starts building an expression tree. As long as you haven't called some of the methods ToList, FirstOrDefault, ... you are not actually hitting your database.
Oh wait! a new problem: now the Find method is gone!
Oops, you are right, return IDbSet<Book> instead of IQueryable<Book>. I have updated my answer to reflect this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.