Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 仅实体框架属性描述符集合字段值_Asp.net Mvc 3_Entity Framework_Edmx_Typedescriptor_Propertydescriptor - Fatal编程技术网

Asp.net mvc 3 仅实体框架属性描述符集合字段值

Asp.net mvc 3 仅实体框架属性描述符集合字段值,asp.net-mvc-3,entity-framework,edmx,typedescriptor,propertydescriptor,Asp.net Mvc 3,Entity Framework,Edmx,Typedescriptor,Propertydescriptor,。。您好,我正在尝试从以下任何实体连接一些列值: var valor = ""; PropertyDescriptorCollection objProperties = TypeDescriptor.GetProperties(obj); foreach (PropertyDescriptor objProperty in objProperties) { if (objProperty.Name != "Audi

。。您好,我正在尝试从以下任何实体连接一些列值:

var valor = "";

        PropertyDescriptorCollection objProperties = TypeDescriptor.GetProperties(obj);


        foreach (PropertyDescriptor objProperty in objProperties)
        {

            if (objProperty.Name != "AuditoriaUC" && objProperty.Name != "AuditoriaFC"
                && objProperty.Name != "AuditoriaIPC" && objProperty.Name != "AuditoriaUM"
                && objProperty.Name != "AuditoriaFM" && objProperty.Name != "AuditoriaIPM"
                && objProperty.Name != "AuditoriaEliminado")
            {
                valor = valor + " " + objProperty.Name + ": " + Convert.ToString(objProperty.GetValue(obj));
            }
        }

        return valor;
但是,它也向我显示了列引用。换句话说,它也会在最后打印:

"ArchivosAdjuntos:System.Data.Objects.DataClasses.EntityCollection`1[XXX.MyProject.Model.Entities.ArchivosAdjuntos] 
 CorrelativoActualPorPeriodo: XXX.MyProject.Model.Entities.CorrelativoActualPorPeriodo
 CorrelativoActualPorPeriodoReference: System.Data.Objects.DataClasses.EntityReference`1[XXX.MyProject.Model.Entities.CorrelativoActualPorPeriodo] 
 EntityState: Modified 
 EntityKey: System.Data.EntityKey"

我只想返回列值,这可以通过将最后一列值与硬编码字符串进行比较来实现,从而中断foreach。但是我真的很想知道是否有更好的方法。

首先可以使用Type类的GetProperties方法,它允许一些过滤,比如只返回公共属性:

PropertyInfo[] props = obj.GetType().GetProperties(BindingFlags.Public 
                                                   | BindingFlags.Instance);
然后,您可以使用LINQ筛选引用类型属性。例如,您可以删除所有引用类型属性(字符串除外)并对其进行迭代:

var valueProps = props.Where(p => !p.PropertyType.IsClass 
                                || p.PropertyType == typeof(string))
valueProps.ToList().ForEach(p => 
                           valor += p.Name + ": " 
                                 + (p.GetValue(obj) ?? "null"));
最后,对于使用硬编码列表排除的属性,可以将该硬编码属性名称列表添加到上面的筛选表达式中。但是,您也可以考虑在要排除的字段中添加属性,并从属性列表中移除包括该属性在内的任何属性。 这就是属性

[AttributeUsage(AttributeTargets.Property)]
public class ExcludeThisPropertyAttribute: Attribute
{
}
你可以把它设置在你的课堂上,就像

public class YourClass
{
    public string PropertyThatIsIncluded { get; set; }
    public int ThisIsIncludedToo { get; set; }
    //more value type properties to be included ...

    [ExcludeThisProperty]
    public string AuditoriaUC { get; set; }
    [ExcludeThisProperty]
    public string AuditoriaFC { get; set; }
    //more value type properties explicitly excluded ...

    //reference type properties like this one will be automatically excluded
    public CorrelativoActualPorPeriodo CorrelativoActualPorPeriodo { get; set; }
}
然后,将更新用于提取值类型属性的表达式,以排除包含该属性的表达式:

var valueProps = props.Where(p => 
               p.GetCustomAttributes(typeof(ExcludeThisPropertyAttribute), true) == 0
               &&(!p.PropertyType.IsClass 
                    || p.PropertyType == typeof(string)))    

在您的例子中,您似乎使用的是实体框架,因此如果您的类是由EF生成的,那么为了能够在属性上设置属性,您需要遵循一种类似于使用

的方法,对属性类使用一种很酷的、有点过火的方法,但它起到了作用,谢谢