[英]Dapper query object with a child of IEnumerable
I am trying query all countries and in each country object it would fill up the provinces. 我正在尝试查询所有国家,并且在每个国家(地区)对象中,它将填满各省。
I have the following Classes 我有以下课程
public class Country
{
public int Countryid { get; set; }
public string CountryName { get; set; }
public IEnumerable<Province> Provinces { get; set; }
}
public class Province
{
public int ProvinceId { get; set; }
public string ProvinceName { get; set; }
}
public IEnumerable<Country> GetCountries()
{
var query = @"
SELECT [Country].[CountryId], [Country].[Name] as CountryName, [Province].[ProvinceId], [Province].[Name] as ProvinceName
FROM [Province]
RIGHT OUTER JOIN [Country] ON [Province].[CountryId] = [Country].[CountryId]
WHERE [Country].[CountryId] > 0";
return _connection.Query<Country, Province, Country>(query, (country, province) =>
{
country.Provinces = country.Provinces.Concat(new List<Province> { province });
return country;
}, null);
}
The error that I get is the following: 我得到的错误如下:
System.ArgumentException: 'When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn' System.ArgumentException:'在使用多重映射API时,如果您具有Id参数名称之外的其他键,请确保设置splitOn参数:splitOn'
I have been following this example: 我一直在关注这个例子:
https://gist.github.com/Lobstrosity/1133111 https://gist.github.com/Lobstrosity/1133111
From my perspective, I don't see what I did much different besides mine being an outer join which I think should not matter, the result format is about the same. 从我的角度来看,除了我认为是无关紧要的外部联接之外,我看不出有什么不同,结果格式大致相同。
Why do I need the split on and can this work without it? 为什么我需要拆分,如果没有拆分,它可以工作吗?
IIRC, I did something like this. IIRC,我做了这样的事情。
var query = @"
SELECT [Country].[CountryId], [Country].[Name] as CountryName, [Province].[ProvinceId], [Province].[Name] as ProvinceName
FROM [Province]
RIGHT OUTER JOIN [Country] ON [Province].[CountryId] = [Country].[CountryId]
WHERE [Country].[CountryId] > 0";
List<Country> countries = new List<Country>();
_connection.Query<Country, Province, Country>(query, (country, province) =>
{
Country lastCountry = countries.FirstOrDefault(d => d.CountryId == country.Id);
if(lastCountry == null)
{
countries.Add(country);
lastCountry = country;
}
lastCountry.Provinces = lastCountry.Provinces.Concat(new List<Province> { province });
return lastCountry;
}, null);
return countries;
I typed this out in LinqPad, so you will need to debug and check it is correct, been a long time since I used Dapper in anger 我是在LinqPad中输入的,所以您需要调试并检查其是否正确,因为我在愤怒中使用Dapper已有很长时间了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.