I would like to create a general query from a general class T. Is there a way to do something like this using reflection or something else?
public class DAO<T> where T : class { protected ObjectSet<T> Entities { get { return myContextThatIsInSomewhere.CreateObjectSet<T>(); } } public IList<T> SelectBy(object fields) { if (fields == null) { throw new ArgumentNullException("fields"); } var query = from e in this.Entities select e; foreach (var field in fields.GetType().GetFields()) { query = from e in this.Entities // Do something like that: where e.(field.Name) == field.GetValue() select e; } return query.ToList(); } }
Make SelectBy take Expression<Func<T, bool>> (call it predicate ) and then you can just say
SelectBy
Expression<Func<T, bool>>
predicate
var query = this.Entities.Where(predicate);
You can pass an instance of Expression<Func<T, bool>> by saying
x => x.Foo == foo
eg.
Source: https://habr.com/ru/post/1392699/More articles:How can I find SharePoint list fields from a database in SharePoint 2010? - xmlUpdating a GridView after UpdateMethod in UpdatePanel - c #Match up to a specific word in a multiline string - c #How to get all public photos from Geotagged from Flickr? - javascriptUpdate two divs with one AJAX response - jsonCCLiquid displays my black screen - objective-chttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1392701/using-java-desktopopen-file-f-and-knowing-that-the-application-is-done&usg=ALkJrhhDotLGig9NH9b3fxItpymp8klHBQJIRA Integration - jiraConvert SQL to LINQ query - c #How do I pass a cookie session to play through Uploadify? - javaAll Articles