动态表达式 LINQ Select - Select 来自嵌套集合的许多
Dynamic Expressions LINQ Select - SelectMany from nested collection
将此示例 class 结构考虑在内 -
public class Apprentice
{
public Guid Id { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public virtual ICollection<ApprenticeAddress> Addresses { get; set; }
}
public class ApprenticeAddress
{
public Guid Id { get; set; }
public Guid ApprenticeId { get; set; }
public virtual Apprentice Apprentice { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Town { get; set; }
public Guid CountyId { get; set; }
public virtual County County { get; set; }
public string PostCode { get; set; }
public bool IsPrimaryAddress { get; set; }
public Guid AddressTypeId { get; set; }
public virtual AddressType AddressType { get; set; }
}
根据上述文档和提供的示例 class 结构,我一直在努力编译动态 select 或 selecting 运行时未知的随机属性。我一直遇到的主要问题是 select 说 AddressLine1 属性 链接到返回学徒的任何地址。
此示例 LINQ select 将执行我需要执行的操作,但任何人都可以帮助将其转换为数据对象初始化字符串吗?
var r = repo.GetAll().ToList().Select(x =>
new
{
x.FamilyName,
addresses = x.Addresses.SelectMany(y => y.AddressLine1)
});
更新
如果我使用下面的代码和传递给 Select 扩展方法的数据对象初始化字符串,我会得到我想要的匿名对象 -
var whereTxt = "Active";
var selectTxt = "new (GivenName AS GivenName,FamilyName AS FamilyName)";
var repo = Storage.DataContext.GetRepository<Apprentice>();
return repo.GetAll().Where(whereTxt).Select(selectTxt).AsQueryable();
我遇到的问题是确定从嵌套集合中检索特定属性(在运行时未知)的语法
这可以使用 System.Linq.Dynamic.Core 轻松完成:
var data = new[]
{
new Apprentice { FamilyName = "f", Addresses = new [] { new ApprenticeAddress { AddressLine1 = "address x" }, new ApprenticeAddress { AddressLine1 = "address y" } } }
}.AsQueryable();
var result = data.Select(x => new { x.FamilyName, Addresses = x.Addresses.Select(y => y.AddressLine1) });
Console.WriteLine("result: " + JsonConvert.SerializeObject(result, Formatting.Indented));
var resultDynamic = data.Select("new (FamilyName as FamilyName, Addresses.Select(AddressLine1) as Addresses)");
Console.WriteLine("resultDynamic: " + JsonConvert.SerializeObject(resultDynamic, Formatting.Indented));
有关完整的工作示例,请参阅 https://github.com/StefH/System.Linq.Dynamic.Core.TestApps
中的 ConsoleApp2
注意Addresses.SelectMany(y => y.AddressLine1)
是错误的,这里是从地址中选择字符。
将此示例 class 结构考虑在内 -
public class Apprentice
{
public Guid Id { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public virtual ICollection<ApprenticeAddress> Addresses { get; set; }
}
public class ApprenticeAddress
{
public Guid Id { get; set; }
public Guid ApprenticeId { get; set; }
public virtual Apprentice Apprentice { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string Town { get; set; }
public Guid CountyId { get; set; }
public virtual County County { get; set; }
public string PostCode { get; set; }
public bool IsPrimaryAddress { get; set; }
public Guid AddressTypeId { get; set; }
public virtual AddressType AddressType { get; set; }
}
根据上述文档和提供的示例 class 结构,我一直在努力编译动态 select 或 selecting 运行时未知的随机属性。我一直遇到的主要问题是 select 说 AddressLine1 属性 链接到返回学徒的任何地址。
此示例 LINQ select 将执行我需要执行的操作,但任何人都可以帮助将其转换为数据对象初始化字符串吗?
var r = repo.GetAll().ToList().Select(x =>
new
{
x.FamilyName,
addresses = x.Addresses.SelectMany(y => y.AddressLine1)
});
更新
如果我使用下面的代码和传递给 Select 扩展方法的数据对象初始化字符串,我会得到我想要的匿名对象 -
var whereTxt = "Active";
var selectTxt = "new (GivenName AS GivenName,FamilyName AS FamilyName)";
var repo = Storage.DataContext.GetRepository<Apprentice>();
return repo.GetAll().Where(whereTxt).Select(selectTxt).AsQueryable();
我遇到的问题是确定从嵌套集合中检索特定属性(在运行时未知)的语法
这可以使用 System.Linq.Dynamic.Core 轻松完成:
var data = new[]
{
new Apprentice { FamilyName = "f", Addresses = new [] { new ApprenticeAddress { AddressLine1 = "address x" }, new ApprenticeAddress { AddressLine1 = "address y" } } }
}.AsQueryable();
var result = data.Select(x => new { x.FamilyName, Addresses = x.Addresses.Select(y => y.AddressLine1) });
Console.WriteLine("result: " + JsonConvert.SerializeObject(result, Formatting.Indented));
var resultDynamic = data.Select("new (FamilyName as FamilyName, Addresses.Select(AddressLine1) as Addresses)");
Console.WriteLine("resultDynamic: " + JsonConvert.SerializeObject(resultDynamic, Formatting.Indented));
有关完整的工作示例,请参阅 https://github.com/StefH/System.Linq.Dynamic.Core.TestApps
中的ConsoleApp2
注意Addresses.SelectMany(y => y.AddressLine1)
是错误的,这里是从地址中选择字符。