You need to create a method whose input parameter is a class

public void TablesJoin<T>(List<T> coll,object MyClass, string sql, string splitOn) { using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DBmodel"].ConnectionString)) { db.Open(); var lookup = db.Query<T, MyClass, T>(sql, (_par_one, _par_two) => { _par_one.MyClass = _par_two; return _par_one; }, splitOn: splitOn).AsQueryable(); var result = lookup.ToList(); coll = result; } } 

error in object MyClass and subsequent mention of this class

Plus the question: is the code correct in the situation with the universal List ??
PS by using Dapper

    1 answer 1

    Add another generic parameter (this is a type parameter)

     public List<T> TablesJoin<T, MyClass>(string sql, string splitOn) { using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["DBmodel"].ConnectionString)) { db.Open(); var lookup = db.Query<T, MyClass, T>(sql, (_par_one, _par_two) => { // не уверен, что должен делать этот код, но скорее всего вам // придется добавить ограничение на T - // т.к. ожидается наличие свойства MyClass _par_one.MyClass = _par_two; return _par_one; }, splitOn: splitOn).AsQueryable(); return lookup.ToList(); // coll = result; <-- это меняет локальную ссылку на список // а не возвращает список наружу. // используйте return, чтобы вернуть объект } } 

    and call with a specific class.

    • Question about the use of the second transmitted class In part _par_one.**MyClass** = _par_two; As you said, you probably need to add a limitation. Visual Studio shows an error. How to solve this problem correctly? - Levon Sargsyan
    • This code must perform a specific SQL query and receive data from the database, which will be recorded in the LIST collection - Levon Sargsyan
    • @LevonSargsyan I just don’t quite understand what you mean by _par_one.**MyClass** . This is a property of the _par_one object, whose name is the same as the class name MyClass. Those. if TablesJoin<SomeClass1, SomeClass2> was called, then you need to set the _par_one.SomeClass2 property? Or something different? - PashaPash