Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
i have several tables in my sql database. all the table contains branch id. i can filter the results in my controller action like
var members=db.Members.Where(x=>x.BranchID=1)

i have to do this in every action.

is there any other method to do so ?
Posted

You could just create a helper class that does it for you, then call that each time you want it.

So you could have a static helper with various methods.

Such as:

GetBrandOneMembers()<br />
GetBrandTwoMembers()<br />
GetMembersForBrans(List<int> brandIds)</int>


etc.
 
Share this answer
 
Hi,

Use Static List :

C#
public static IEnumareble<member> members= new Lazy<ienumareble><member>>(() => GetMember );
private static IEnumareble<member> GetMember()
{
using ( var db=new YourDBContext()){ 
return db.Members.Where(x=>x.BranchID=1).ToArray();
}
}
</member></member></ienumareble></member>
 
Share this answer
 
you can create an iterface
C#
public interface IBranchable
{
 int BranchID {get;set;}
}


then you need to make every entity class that has BranchID to implement IBranchable interface
C#
public partial Member : IBranchable
{
}
// and every other entity that have BranchId ...


and at the end you can can make an extension method to filter

C#
public static ExtensionsMethods 
{
   public static IEnumerable<IBranchable> GetByBranchId(this IEnumerable<IBranchable> source, int branchId)
   {
       return source.Where(x=> x.BranchID == branchId);
   }
   public static IEnumerable<IBranchable> GetByBranchId1(this IEnumerable<IBranchable> source)
   {
       return source.Where(x=> x.BranchID == 1);
   }
}


usually i act like this and the code is a lot more clear and readable

usage :
db.Members.GetByBranchId(1)
db.OtherEntity.GetByBranchId(1)



UPDATE :
if your controller uses directly entity framework, then you could create a wrapper to expose entity sets pre-filtered by your branchId

C#
public class InternalContext: IDisposable
{
    protected bool disposed = false;
    private YourEntityContext db;
    public int BranchId { get; private set;}

    public InternalContext(int branchID)
    {
        db = new YourEntityContext();
        BranchId = branchID;
    }

    public IEnumerable<Member> Members 
    { 
        get { 
            return db.Members.GetByBranchId(BranchID);
        }
    }
    // and so on

    protected virtual void Dispose(bool disposing)
    {
        if (disposed)
        {
            return;
        }

        if (disposing)
        {
            db.Dispose();
        }

        disposed = true;
    }

    public void IDisposable.Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}


usage :
using(var db = new InternalContext(1))
{
var members = db.Members;
}
 
Share this answer
 
v5
Comments
Jini Jose 16-Sep-14 6:26am    
katanakensei,

then what about if there is 100 branches and 100 tables each having branchid as a field ?
nrgjack 16-Sep-14 6:32am    
the problem is that you need to implement IBranchable in 100 class ??
if it is the case, and you are using entity framework you can modify the entity class generator (file .TT) to add ": IBranchable" to the class that have a member with the same name, but it is a bit more complicated than write 100 row..
hi Suvabratha Roy,

I am creating the context from database.

Member dataaset from context.
public DbSet<member> Members { get; set; }

is there any way to filter it here ?
like
public DbSet<member> Members.Where(x=>x.BranchID==1) { get; set; }

[Update by Suvabrata]
C#
private IDbSet<member> _Members;
public IEnumareble<member> Members {
get
{

return this._Members.Where(x=> x.BranchID==1).ToArray();
}
}
 
Share this answer
 
v3
Comments
Suvabrata Roy 16-Sep-14 6:06am    
Yes you can... but your syntax is wrong.



But to infrom me please comment.
Jini Jose 16-Sep-14 6:13am    
this is not the syntax.
i just asked whether it is possible in this way or not ?
Suvabrata Roy 16-Sep-14 6:14am    
I have updated your solution check it
Jini Jose 16-Sep-14 6:19am    
getting the below error

Error 1 'System.Data.Entity.DbSet<models.member>' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'System.Data.Entity.DbSet<models.member>' could be found (are you missing a using directive or an assembly reference?)
Suvabrata Roy 16-Sep-14 6:34am    
Try the modified one in updated section...
that is not possible for every actions. there may be 100's of action results. so creating helper class for each action is not good
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900