Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

query into complex object using dapper

public class Customer {
    public int Id { get; set; }
    public string Name { get; set; }
    public int AddressId { get; set; }  
    public int ContactId { get; set; }
    public Address Address { get; set; }
    public Contact Contact { get; set; }
}

public class Address {
    public int Id { get; set; }
    public string Address1 {get;set;}
    public string Address2 {get;set;}
    public string City {get;set;}
    public string State {get;set;}
    public int ZipCode {get;set;}
    public IEnumerable<Customer> Customer {get;set;}
}

public class Contact {
    public int Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<Customer> Customer {get;set;}
}

using (var conn = GetOpenConnection())
{
    var query = _contextDapper
        .Query<Customer, Address, Contact, Customer>($@"
            SELECT c.Id, c.Name, 
                c.AddressId, a.Id, a.Address1, a.Address2, a.City, a.State, a.ZipCode,
                c.ContactId, ct.Id, ct.Name
            FROM Customer c
            INNER JOIN Address a ON a.Id = c.AddressId
            INNER JOIN Contact ct ON ct.Id = c.ContactId", 
            (c, a, ct) =>
            {
                c.LogType = a;
                c.Contact = ct;
                return c; 
            }, splitOn: "AddressId, ContactId")
        .AsQueryable();

    return query.ToList();          
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# use readonly array as method default 
Csharp :: c# code for simplex method 
Csharp :: c# order by descending on 2 values 
Csharp :: array hw exercise 
Csharp :: Request ID: XPBBR4XG1UWuX6fWF08_-jzYkrommVJjO7Os50CTYuZmiw7kMsFUkw== 
Csharp :: how to connect google play services in unity 
Csharp :: nest elasticsearch date reange c# .net 
Csharp :: asp net route attribute vs httpget 
Csharp :: how to unfocus a richtextbox windows forms 
Csharp :: ASP.MVC display image from SqlServer 
Csharp :: Devexpress MVC Gridview BinaryImage Picture 
Csharp :: class combining 
Csharp :: worsening 
Csharp :: c# create empty file if not exists 
Csharp :: binaural generator 
Csharp :: wpf user parent controller datacontext 
Csharp :: C# Func Delegate 
Csharp :: temp^late php table for mysql 
Csharp :: how to specify order of test in c# 
Csharp :: generate an mvc controller when dotnet core command line 
Csharp :: unity int inputfield value 
Csharp :: compare 0001/01/01 in c# 
Csharp :: Difference between UnitOfWork and Repository Pattern 
Csharp :: how to add the ssl certificate in vb.net application 
Csharp :: how to run a console app in another app c# 
Csharp :: IdentityServer vs JWT vs OAuth? 
Csharp :: Unity make a homing object 
Csharp :: internal static object ds 
Csharp :: .net SaveChanges vs update difference 
Csharp :: Unable to Write json variable c# getting an error 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =