Biraz daha gelişmiş haritalama sonra benim previous question :)Zarif ara haritalama
Tablo:
create table [Primary] (
Id int not null,
CustomerId int not null,
CustomerName varchar(60) not null,
Date datetime default getdate(),
constraint PK_Primary primary key (Id)
)
create table Secondary(
PrimaryId int not null,
Id int not null,
Date datetime default getdate(),
constraint PK_Secondary primary key (PrimaryId, Id),
constraint FK_Secondary_Primary foreign key (PrimaryId) references [Primary] (Id)
)
create table Tertiary(
PrimaryId int not null,
SecondaryId int not null,
Id int not null,
Date datetime default getdate(),
constraint PK_Tertiary primary key (PrimaryId, SecondaryId, Id),
constraint FK_Tertiary_Secondary foreign key (PrimaryId, SecondaryId) references Secondary (PrimaryId, Id)
)
Sınıflar:
public class Primary
{
public int Id { get; set; }
public Customer Customer { get; set; }
public DateTime Date { get; set; }
public List<Secondary> Secondaries { get; set; }
}
public class Secondary
{
public int Id { get; set; }
public DateTime Date { get; set; }
public List<Tertiary> Tertiarys { get; set; }
}
public class Tertiary
{
public int Id { get; set; }
public DateTime Date { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
}
hepsiyle doldurmak için seçme birini kullanmak mümkün mü? Böyle bir şey: o zaman
const string sqlStatement = @"
select
p.Id, p.CustomerId, p.CustomerName, p.Date,
s.Id, s.Date,
t.Id, t.Date
from
[Primary] p left join Secondary s on (p.Id = s.PrimaryId)
left join Tertiary t on (s.PrimaryId = t.PrimaryId and s.Id = t.SecondaryId)
order by
p.Id, s.Id, t.Id
";
Ve:
IEnumerable<Primary> primaries = connection.Query<Primary, Customer, Secondary, Tertiary, Primary>(
sqlStatement,
... here comes dragons ...
);
Edit1 - ve her öğe için bir sorgu gerçekleştirmek, ama sadece merak - I (> foreach üçlü sistem foreach ikinciller) iç içe geçmiş iki döngüler ile bunu yapabileceğimi Tek bir veritabanı çağrısı ile yapılabilir.
Düzenleme2 - belki de buradaki QueryMultiple yöntemi uygun olabilir, ancak doğru bir şekilde anlarsam birden çok seçim deyimine ihtiyacım olur. Benim gerçek hayattaki örnekte, seçimin daha sonra 20 parametresi (burada, cümlede), arama parametresinin null olabileceği için, tüm sorgularda ifadelerin olduğu tümleri tekrarlamak istemiyorum ...