有没有办法首先从 ef 代码中的 entry.Collection 获取每个值?
Is there a way to get each value from entry.Collection in ef code first?
我目前有以下代码:
System.Reflection.PropertyInfo[] names = _entity.GetType().GetProperties();
var entry = this.Entry(_entity);
foreach (var property in names)
{
var m = entry.Member(property.Name);
if (m is DbPropertyEntry)//simple property
{
var p = entry.Property(property.Name);
currentVal.Add(p.CurrentValue.ToString());
}
if (m is DbReferenceEntry)//navigation to single object
{
var r = entry.Reference(property.Name);
currentVal.Add(r.CurrentValue.ToString());
}
if (m is DbCollectionEntry)//navigation to collection
{
var c = entry.Collection(property.Name);
currentVal.Add(c.CurrentValue.ToString());
}
}
我想从与我的实体关联的集合中获取每个字段值,但 entry.Collection(property.Name) 仅 returns 集合名称而不是该集合中的字段这样作为它的名称、id 等。因此我实际上无法获取集合中的当前值。
非常感谢任何建议!
谢谢
编辑:
我正在使用的实体设置如下:
public class Project
{
public int Id { get; set; } // PK
public string Name { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int MarketId { get; set; } // FK
[ForeignKey("MarketId")]
public Market Market { get; set; } // FK Nav
public ICollection<Team> Teams { get; set; } // Many to Many Nav
}
我希望查看的相关集合是 Teams,我正在对此实体进行审计,当创建新项目时,我想查看团队中相关项目 ID 的更新值 table
好吧,我想你想创建一个基于字符串数组的 Select
,这些字符串是属性的名称。我对吗?。
您是否尝试过使用 Linq Dynamic? http://dynamiclinq.azurewebsites.net/。它允许您使用字符串表达式构造 LINQ 查询。
var result = myQuery
.Select("new (Field1, Field2)");
//in your case
var result2 = myQuery
.Select("new (" + string.Join(fields, ', ') + ")");
对你有帮助吗?
如果我对你的问题的理解正确,那么你想要遍历实体集合中的项目。一种方法是将 c.CurrentValue 强制转换为 IEnumerable,然后使用 foreach 循环遍历可枚举对象。
foreach(var x in (IEnumerable) c.CurrentValue){
// do something with the entity here
}
这将为您提供集合中的实体。根据您想对它们做什么,您可以 ToString() 实体本身或递归使用您发布的代码来获取它们的 属性 值。
需要注意的一件事是 EF 不会急切地加载集合属性。因此可能是该集合在内存中为空,而在数据库中不为空。 DbCollectionEntry 类型有一个 IsLoaded 属性 来检查这个。
我目前有以下代码:
System.Reflection.PropertyInfo[] names = _entity.GetType().GetProperties();
var entry = this.Entry(_entity);
foreach (var property in names)
{
var m = entry.Member(property.Name);
if (m is DbPropertyEntry)//simple property
{
var p = entry.Property(property.Name);
currentVal.Add(p.CurrentValue.ToString());
}
if (m is DbReferenceEntry)//navigation to single object
{
var r = entry.Reference(property.Name);
currentVal.Add(r.CurrentValue.ToString());
}
if (m is DbCollectionEntry)//navigation to collection
{
var c = entry.Collection(property.Name);
currentVal.Add(c.CurrentValue.ToString());
}
}
我想从与我的实体关联的集合中获取每个字段值,但 entry.Collection(property.Name) 仅 returns 集合名称而不是该集合中的字段这样作为它的名称、id 等。因此我实际上无法获取集合中的当前值。
非常感谢任何建议! 谢谢
编辑:
我正在使用的实体设置如下:
public class Project
{
public int Id { get; set; } // PK
public string Name { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public int MarketId { get; set; } // FK
[ForeignKey("MarketId")]
public Market Market { get; set; } // FK Nav
public ICollection<Team> Teams { get; set; } // Many to Many Nav
}
我希望查看的相关集合是 Teams,我正在对此实体进行审计,当创建新项目时,我想查看团队中相关项目 ID 的更新值 table
好吧,我想你想创建一个基于字符串数组的 Select
,这些字符串是属性的名称。我对吗?。
您是否尝试过使用 Linq Dynamic? http://dynamiclinq.azurewebsites.net/。它允许您使用字符串表达式构造 LINQ 查询。
var result = myQuery
.Select("new (Field1, Field2)");
//in your case
var result2 = myQuery
.Select("new (" + string.Join(fields, ', ') + ")");
对你有帮助吗?
如果我对你的问题的理解正确,那么你想要遍历实体集合中的项目。一种方法是将 c.CurrentValue 强制转换为 IEnumerable,然后使用 foreach 循环遍历可枚举对象。
foreach(var x in (IEnumerable) c.CurrentValue){
// do something with the entity here
}
这将为您提供集合中的实体。根据您想对它们做什么,您可以 ToString() 实体本身或递归使用您发布的代码来获取它们的 属性 值。
需要注意的一件事是 EF 不会急切地加载集合属性。因此可能是该集合在内存中为空,而在数据库中不为空。 DbCollectionEntry 类型有一个 IsLoaded 属性 来检查这个。