Search code examples
c#dapper

How do I assign a list retrieved by Dapper to the properties in my class model?


I have a model class that has a number of public properties (name, address, etc.). These properties exactly match the columns in a SQL database. I have a form that permits a user to put in a Client ID number, and on click, passes the Client ID to a method in a business logic class that in turn calls a general data access class that uses a Dapper Query to return the client record from the SQL database. This result is bound to a datagridview, and it works fine.

(So this works fine: Form > BLClass > DataAccessClass > BLClass > Form)

However, in my business logic class, I would like to have access to all of the properties in my model class, so I can generate reports and run some calculations. I can create a new, empty instance of my model class and all of the properties appear after the dot in intellisense, but when I try to access the actual list returned by the DataAccess Class, which is of type List, I can't set or get any of the properties in the model class.

Note: My business class method started as a public void method, but in order to return the list to the form for use in the datagridview I had to make the method "public List CreateReport(int ClientID);" I don't care about displaying the result on the form - I can always populate that separately. I just need to be able to populate the properties of the model class inside the business logic class so I can use them in a report.

//The model class
public class ClientModel
{

    public int ClientID { get; set; }
    public string ClientPosition { get; set; }
    public string Fname { get; set; }
    public string MName { get; set; }
    public string LName { get; set; }
    //etc.  
}

//The business logic class 

public void CreateReport(int ClientID);

List<ClientModel> client = DataConfig.Connection.GetClientByID(ClientID).ToList();

string ClientName = (client.FName + " " + client.MName + " " + client.LName).ToUpper();
string ClientNameFooter = client.FName + " " + client.MName + " " + client.LName;

//Etc., with the int's and strings eventually replacing bookmarked sections in a Word template using Word.Interop.

When I try to access client.Fname, I get the error "List' does not contain a definition for 'FName' and no accessible extension method 'FName' accepting a first argument of type 'List' could be found."

I tried assigning the list variable "client" to an empty instance of the Client model, but I got the error "Cannot implicitly convert type 'ReportGenerator.ClientModel' to 'System.Collections.Generic.List.ReportGenerator.ClientModel'.

New to C# and Dapper, so this could be something completely obvious I'm missing with the list object.


Solution

  • Your variable client is a list. The is no Fname on the list. You can do

    client[0].Fname
    

    or index it in another way.

    Since you are new, here are some other hints:

    • Do yourself and those who need to maintain the code after you a favour and use long variable/property names. So FirstName instead of Fname.
    • Be consistent. Don't call one property Fname and another MName.
    • By convention local variables start with small letters: clientName not ClientName.
    • Use string interpolation:

      string clientName = "{client.FName} {client.MName} {client.LName}".ToUpper(); 
      

    EDIT: Looking at your question again I think you want to use FirstOrDefault or Single. FirstOrDefault will take the first item in your list or default (null) if the list is empty. Single will take exactly one item if there is one and otherwise throw an exception.

    ClientModel client = DataConfig.Connection.GetClientByID(ClientID).Single();
    

    Then you will not have to iterate or index into the list.