1

I have multiple classes which I use in different partial pages. I use Dapper for SQL Connection. From DB I get single result set with all properties.

How can I bind data to multiple class? Is there any chances to get this work? or I need to create a single class with all properties?

My models:

public class Class1{
     public int prop1 {get;set;}
     public int prop2 {get;set;}
}

public class Class2{
     public int prop3 {get;set;}
     public int prop4 {get;set;}
}
public class Class3{
     public int prop5 {get;set;}
     public int prop6 {get;set;}
}

ResultSet from DB:

PrimarykeyCol  prop1 prop2  prop3  prop4  prop5  prop6
1
  • 1
    Here is an example of using Dapper's Multimap Query with SplitOn Argument specified. It appears to be similar to what you are attempting to accomplish.
    – mreyeros
    Oct 28, 2020 at 11:22

1 Answer 1

1

If I understand you correctly there is no relation between the three classes. Normally multimapping includes relations. For the multi-query to work you need a return type, so I would definitely declare a wrapper for the three classes like this:

public class Wrapper
{
    public Class1 Class1 {get;set;}
    public Class2 Class2 {get;set;}
    public Class3 Class3 {get;set;}
}

var resultList = conn.Query<Class1, Class2, Class3, Wrapper>(query, 
                 (c1, c2, c3) => {
                     return new Wrapper {Class1=c1, Class2=c2, Class3=c3};
                 },
                 splitOn: "prop3,prop5"
                 ).AsQueryable();
1
  • What would be the approach if I am using StoredProcedure?
    – Ankita
    Oct 29, 2020 at 6:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.