3

I cant built a complex object with a query. How I do?

public class Person
{
    public long Id { get; set; }
    public string Name { get; set; }
    public Contact Contact { get; set; }
}

public class Contact
{
    public long Id { get; set; }
    public string FoneNumber { get; set; }
}
1

As you have wrote before, use the Join Method to join with the "Contact" table,

var row = db.Query("Person")
            .Select(
                "Person.Id",
                "Person.Name",
                "Contact.Id as ContactId",
                "Contact.FoneNumber as FoneNumber"
            )
            .Join("Contact", "Person.Id", "Contact.PersonId")
            .Where("Person.Id", 1)
            .FirstOrDefault();
|improve this answer|||||
0

You can use "Multi-Mapping" feature of Dapper.

    [Test]
    public void Test_Multi_Mapping()
    {
        using (var conn = new SqlConnection(@"Data Source=.\sqlexpress; Integrated Security=true; Initial Catalog=test"))
        {
            var result = conn.Query<Person, Contact, Person>(
                "select Id = 1, Name = 'Jane Doe', Id = 2, FoneNumber = '800-123-4567'",
                (person, contact) => { person.Contact = contact;
                    return person;
                }).First();

            Assert.That(result.Contact.FoneNumber, Is.EqualTo("800-123-4567"));
        }
    }

You can also use ".QueryMultiple". Read Dapper's documentation, or take a look at the unit tests for more examples.

|improve this answer|||||
  • But I want use sqlkata, because I need use dynamic WHERE clause. – VFB Aug 24 '18 at 14:12
0

My code:

        var compiler = new SqlServerCompiler();
        var db = new QueryFactory(connection, compiler);

        var person = db.Query("Person")
                        .Select("Person.Id", "Person.Name", "Contact.Id", "Contact.FoneNumber")
                        .Join("Contact", "Person.Id", "Contact.PersonId")
                        .Where("Person.Id", 1)
                        .FirstOrDefault<Person>();
|improve this answer|||||

Your Answer

By clicking “Post Your Answerâ€, you agree to our terms of service, privacy policy and cookie policy

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