LINQ Generic Query with inherited base class?

"YOU AND THE ART OF ONLINE DATING" is the only product on the market that will take you step-by-step through the process of online dating, provide you with the resources to help ensure success. Get it now!

If I understand what you want the question you linked to describes (sort of) what you need to do public ImplementationType getOne(Expression singleOrDefaultClause) { ImplementationType entity = null; if (singleOrDefaultClause! = null) { LCFDataContext lcfdatacontext = new LCFDataContext(); //Generic LINQ Query Here entity = lcfdatacontext.GetTable(). SingleOrDefault(singleOrDefaultClause); lcfdatacontext.Dispose(); } return entity; } When you call this method it would look something like note assumption that ConcreteClass DAO has member called Id var myEntity = ConcreteClass.

GetOne(x=>x. Id == myIdVariable) I haven't compiled this so I can't say it's 100% correct, but the idea works. I'm using something similar except the I defined my methods to be generic with a base class to implement the common code Update Can't you just use new to create an instance of the class you need?

If you need something more generic then I think you will have to use reflection to invoke the constructor. Sorry if I misunderstood what you were asking Update in response to comment for additional details Expansion of updating POCO: There are a lot of ways you can do this but one is to get the PropertyInfo from the expression and invoke the setter. (Probably better ways to do this, but I haven't figured one out.) For example it could look something like: protected internal bool Update(Expression> property, TRet updatedValue) { var property = ((MemberExpression)member.

Body). Member as PropertyInfo; if (property! = null) { property.

SetValue(this, updatedValue, null); return true; } return false; } Note: I pulled this (with some other stuff removed) from my code base on a project I am working. This method is part of a base class that all of my POCOs implement. The Editable base class and the base class for my POCOs are in the same assembly so the Editable can invoke this as an internal method I got my methods working but I like yours more because it is more flexible allowing for multiple parameters, but I really really want to keep this in my DAO.

It would be kind of confusing to have all db methods in my DAO except one. Would setting the function to be getOne work? I'm not really sure I understand what you are asking me.

Yes you could set the getOne function to be a generic method, this is actually what I have done although I have taken it a step further and all of the methods are generic. This simplified my UI/BL interface boundary and so far at least is expressive/flexibly enough to cover all of my usage requirements without major changes. If it helps I've included the interface that by BL object implements and exposes to the UI.

My DAL is essentially Nbernate so I don't have anything to show you there public interface ISession : IDisposable { bool CanCreate() where T : class,IModel; bool CanDelete() where T : class, IModel; bool CanEdit() where T : class, IModel; bool CanGet() where T : class, IModel; T Create(IEditable newObject) where T:class,IModel; bool Delete(Expression> selectionExpression) where T : class, IModel; PageData Get(int page, int numberItemsPerPage, Expression> whereExpression) where T : class, IModel; PageData Get(int page, int numberItemsPerPage, Expression> whereExpression, Expression orderBy, bool isAscending) where T : class, IModel; T Get(Expression> selectionExpression) where T : class, IModel; IEnumerable GetAllMatches(Expression> whereExpression) where T : class, IModel; IEditable GetForEdit(Expression> selectionExpression) where T : class, IModel; IEditable GetInstance() where T : class, IModel; IQueryable Query() where T : class, IModel; bool Update(IEditable updatedObject) where T : class, IModel; }.

If I understand what you want the question you linked to describes (sort of) what you need to do. Public ImplementationType getOne(Expression singleOrDefaultClause) { ImplementationType entity = null; if (singleOrDefaultClause! = null) { LCFDataContext lcfdatacontext = new LCFDataContext(); //Generic LINQ Query Here entity = lcfdatacontext.GetTable().

SingleOrDefault(singleOrDefaultClause); lcfdatacontext.Dispose(); } return entity; } When you call this method it would look something like //note assumption that ConcreteClass DAO has member called Id var myEntity = ConcreteClass. GetOne(x=>x. Id == myIdVariable); I haven't compiled this so I can't say it's 100% correct, but the idea works.

I'm using something similar except the I defined my methods to be generic with a base class to implement the common code. Update Can't you just use new to create an instance of the class you need? If you need something more generic then I think you will have to use reflection to invoke the constructor.

Sorry if I misunderstood what you were asking. Update in response to comment for additional details Expansion of updating POCO: There are a lot of ways you can do this but one is to get the PropertyInfo from the expression and invoke the setter. (Probably better ways to do this, but I haven't figured one out.) For example it could look something like: protected internal bool Update(Expression> property, TRet updatedValue) { var property = ((MemberExpression)member.

Body). Member as PropertyInfo; if (property! = null) { property.

SetValue(this, updatedValue, null); return true; } return false; } Note: I pulled this (with some other stuff removed) from my code base on a project I am working. This method is part of a base class that all of my POCOs implement. The Editable base class and the base class for my POCOs are in the same assembly so the Editable can invoke this as an internal method.

I got my methods working but I like yours more because it is more flexible allowing for multiple parameters, but I really really want to keep this in my DAO. It would be kind of confusing to have all db methods in my DAO except one. Would setting the function to be getOne work?

I'm not really sure I understand what you are asking me. Yes you could set the getOne function to be a generic method, this is actually what I have done although I have taken it a step further and all of the methods are generic. This simplified my UI/BL interface boundary and so far at least is expressive/flexibly enough to cover all of my usage requirements without major changes.

If it helps I've included the interface that by BL object implements and exposes to the UI. My DAL is essentially Nbernate so I don't have anything to show you there. Public interface ISession : IDisposable { bool CanCreate() where T : class,IModel; bool CanDelete() where T : class, IModel; bool CanEdit() where T : class, IModel; bool CanGet() where T : class, IModel; T Create(IEditable newObject) where T:class,IModel; bool Delete(Expression> selectionExpression) where T : class, IModel; PageData Get(int page, int numberItemsPerPage, Expression> whereExpression) where T : class, IModel; PageData Get(int page, int numberItemsPerPage, Expression> whereExpression, Expression orderBy, bool isAscending) where T : class, IModel; T Get(Expression> selectionExpression) where T : class, IModel; IEnumerable GetAllMatches(Expression> whereExpression) where T : class, IModel; IEditable GetForEdit(Expression> selectionExpression) where T : class, IModel; IEditable GetInstance() where T : class, IModel; IQueryable Query() where T : class, IModel; bool Update(IEditable updatedObject) where T : class, IModel; }.

That's what I really wanted to avoid. The main issue is because my DAO classes do not have a member called ID. I like to have my object classes separate from database logic classes (DAO) and business logic classes (services).

It just seems odd. Is there no expression or . Command to signify to input a column name/property as a string?

– DOTang Apr 8 '10 at 2:12 actually the point to this is your predicate can be anything that will scope down to a single entity. For instance x=>x.Name == "some Name" && x. Created == "02/03/2010" would be valid for the predicate as long as it returned a single entity.

If you want separate DAO and service classes then you would have to translate the logic in any case. If you want to use the column name and value I'm not sure how you would go about that. – Ken Henderson Apr 8 '10 at 2:35 Is it possible to build a predicate off string parameters?

I am going to guess no since the id in x.Id is a known property. If possible, then I guess it would just be a two step process of building a predicate off the supplied parameters and then putting that predicate into your example above. – DOTang Apr 8 '10 at 2:47 Oh I think I found an answer to that: stackoverflow.Com/questions/125400/generic-linq-query-predicate Looks like just what I need, strings as parameters!

– DOTang Apr 8 '10 at 3:12 Geek, could you expand upon your Update about using reflection? I got my methods working but I like yours more because it is more flexible allowing for multiple parameters, but I really really want to keep this in my DAO.It would be kind of confusing to have all db methods in my DAO except one. Would setting the function to be getOne work?

– DOTang 02/03/20108 at 22:56.

Extension method 'Public Function SingleOrDefault(predicate As System.Linq.Expressions. Func(Of Accomplishment, Boolean))) As Accomplishment' defined in 'System.Linq. Queryable': Value of type 'System.

Accomplishment, Boolean))' cannot be converted to 'System.Linq.Expressions. Extension method 'Public Function SingleOrDefault(predicate As System. Func(Of Accomplishment, Boolean)) As Accomplishment' defined in 'System.Linq.

Enumerable': Value of type 'System. Accomplishment, Boolean))' cannot be converted to 'System.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions