Linq to sql 如何从Linq到Sql中的属性名称中获取属性值?

Linq to sql 如何从Linq到Sql中的属性名称中获取属性值?,linq-to-sql,Linq To Sql,我有通用存储库。有一个方法Save()将实体持久化到数据库中。我想通过Table.GetPropertyValue(“ID”,实体)检查主键——我应该如何实现它 private Table<T> Table { get { return db.GetTable<T>(); } } public void Save(T entity) { if (Table.GetPropertyValue("ID", entity) == 0) {

我有通用存储库。有一个方法Save()将实体持久化到数据库中。我想通过Table.GetPropertyValue(“ID”,实体)检查主键——我应该如何实现它

private Table<T> Table
{
    get { return db.GetTable<T>(); }
}

public void Save(T entity)
{
    if (Table.GetPropertyValue("ID", entity) == 0)
    {
         Add(entity);
    }
    else if (Table.GetOriginalEntityState(entity) == null)
    {
         Table.Attach(entity);
         Table.Context.Refresh(RefreshMode.KeepCurrentValues, entity);
    }
    db.SubmitChanges();
}
私有表
{
获取{return db.GetTable();}
}
公共作废保存(T实体)
{
if(Table.GetPropertyValue(“ID”,实体)==0)
{
添加(实体);
}
else if(Table.GetOriginalEntityState(entity)==null)
{
表.附件(实体);
Table.Context.Refresh(RefreshMode.KeepCurrentValues,entity);
}
db.SubmitChanges();
}
您可以在C#4中使用dynamic:

您可以在C#4中使用dynamic:


您可以使用反射,但对于这个用例,我建议创建一个接口:

public interface IHasId
{
    public int ID{get;}
}

使所有相关表实现此接口(在部分类定义中),然后您可以尝试将
实体
强制转换为IHasId

您可以使用反射,但对于此用例,我建议创建一个接口:

public interface IHasId
{
    public int ID{get;}
}

使所有相关表实现此接口(在部分类定义中),然后您可以尝试将
实体
强制转换为IHasId

假设我有一个定义如下的B类:

public class B
{
    public int Id { get; set; }
}
    public static int GetId(object o)
    {
        var idProperty = o.GetType().GetProperties().FirstOrDefault(p => p.Name == "Id");

        if (idProperty == null)
            throw new ArgumentException("object does not have an Id property", "o");

        if (idProperty.PropertyType.FullName != typeof(Int32).FullName)
            throw new ArgumentException("object has an Id property, but it is not of type Int32", "o");

        return (int)idProperty.GetValue(o, new object[] { });
    }
然后您可以像这样获得id的值:

var b = new B();
b.Id = 60;
int id = GetId(b);
    public static T GetProperty<T>(object o, string propertyName)
    {
        var theProperty = o.GetType().GetProperties().FirstOrDefault(p => p.Name == propertyName);

        if (theProperty == null)
            throw new ArgumentException("object does not have an " +  propertyName + " property", "o");

        if (theProperty.PropertyType.FullName != typeof(T).FullName)
            throw new ArgumentException("object has an Id property, but it is not of type " + typeof(T).FullName, "o");

        return (T)theProperty.GetValue(o, new object[] { });
    }
使用定义如下的
GetId
方法:

public class B
{
    public int Id { get; set; }
}
    public static int GetId(object o)
    {
        var idProperty = o.GetType().GetProperties().FirstOrDefault(p => p.Name == "Id");

        if (idProperty == null)
            throw new ArgumentException("object does not have an Id property", "o");

        if (idProperty.PropertyType.FullName != typeof(Int32).FullName)
            throw new ArgumentException("object has an Id property, but it is not of type Int32", "o");

        return (int)idProperty.GetValue(o, new object[] { });
    }
一个更通用的解决方案是创建如下方法:

var b = new B();
b.Id = 60;
int id = GetId(b);
    public static T GetProperty<T>(object o, string propertyName)
    {
        var theProperty = o.GetType().GetProperties().FirstOrDefault(p => p.Name == propertyName);

        if (theProperty == null)
            throw new ArgumentException("object does not have an " +  propertyName + " property", "o");

        if (theProperty.PropertyType.FullName != typeof(T).FullName)
            throw new ArgumentException("object has an Id property, but it is not of type " + typeof(T).FullName, "o");

        return (T)theProperty.GetValue(o, new object[] { });
    }
公共静态T GetProperty(对象o,字符串propertyName) { var theProperty=o.GetType().GetProperties().FirstOrDefault(p=>p.Name==propertyName); 如果(属性==null) 抛出新ArgumentException(“对象没有”+propertyName+“property”,“o”); if(theProperty.PropertyType.FullName!=typeof(T).FullName) 抛出新ArgumentException(“对象有一个Id属性,但它不是类型“+typeof(T).FullName,“o”); 返回(T)property.GetValue(o,新对象[]{}); } 可以这样称呼:

var b = new B();
b.Id = 60;
int id = GetProperty<int>(b, "Id");
var b=new b();
b、 Id=60;
int id=GetProperty(b,“id”);

假设我的B类定义如下:

public class B
{
    public int Id { get; set; }
}
    public static int GetId(object o)
    {
        var idProperty = o.GetType().GetProperties().FirstOrDefault(p => p.Name == "Id");

        if (idProperty == null)
            throw new ArgumentException("object does not have an Id property", "o");

        if (idProperty.PropertyType.FullName != typeof(Int32).FullName)
            throw new ArgumentException("object has an Id property, but it is not of type Int32", "o");

        return (int)idProperty.GetValue(o, new object[] { });
    }
然后您可以像这样获得id的值:

var b = new B();
b.Id = 60;
int id = GetId(b);
    public static T GetProperty<T>(object o, string propertyName)
    {
        var theProperty = o.GetType().GetProperties().FirstOrDefault(p => p.Name == propertyName);

        if (theProperty == null)
            throw new ArgumentException("object does not have an " +  propertyName + " property", "o");

        if (theProperty.PropertyType.FullName != typeof(T).FullName)
            throw new ArgumentException("object has an Id property, but it is not of type " + typeof(T).FullName, "o");

        return (T)theProperty.GetValue(o, new object[] { });
    }
使用定义如下的
GetId
方法:

public class B
{
    public int Id { get; set; }
}
    public static int GetId(object o)
    {
        var idProperty = o.GetType().GetProperties().FirstOrDefault(p => p.Name == "Id");

        if (idProperty == null)
            throw new ArgumentException("object does not have an Id property", "o");

        if (idProperty.PropertyType.FullName != typeof(Int32).FullName)
            throw new ArgumentException("object has an Id property, but it is not of type Int32", "o");

        return (int)idProperty.GetValue(o, new object[] { });
    }
一个更通用的解决方案是创建如下方法:

var b = new B();
b.Id = 60;
int id = GetId(b);
    public static T GetProperty<T>(object o, string propertyName)
    {
        var theProperty = o.GetType().GetProperties().FirstOrDefault(p => p.Name == propertyName);

        if (theProperty == null)
            throw new ArgumentException("object does not have an " +  propertyName + " property", "o");

        if (theProperty.PropertyType.FullName != typeof(T).FullName)
            throw new ArgumentException("object has an Id property, but it is not of type " + typeof(T).FullName, "o");

        return (T)theProperty.GetValue(o, new object[] { });
    }
公共静态T GetProperty(对象o,字符串propertyName) { var theProperty=o.GetType().GetProperties().FirstOrDefault(p=>p.Name==propertyName); 如果(属性==null) 抛出新ArgumentException(“对象没有”+propertyName+“property”,“o”); if(theProperty.PropertyType.FullName!=typeof(T).FullName) 抛出新ArgumentException(“对象有一个Id属性,但它不是类型“+typeof(T).FullName,“o”); 返回(T)property.GetValue(o,新对象[]{}); } 可以这样称呼:

var b = new B();
b.Id = 60;
int id = GetProperty<int>(b, "Id");
var b=new b();
b、 Id=60;
int id=GetProperty(b,“id”);

我需要更通用的解决方案表。GetPropertyValue(“Some_Property”,entity)。可以通过扩展方法始终存在
o.GetType().GetProperty(propertyName)太多:PI需要更通用的解决方案表。GetPropertyValue(“Some_Property”,entity)。可以通过扩展方法始终存在
o.GetType().GetProperty(propertyName)太:P@eldar,在这种情况下,使用klausbyskov或Rob的解决方案@在这种情况下,使用克劳斯比斯科夫或罗布的解决方案+1、这是最好的解决方案<代码>动态
和反射不是静态类型安全的——这是。就我个人而言,我更喜欢你的动态解决方案-闪亮:)哈哈,好吧,选择太多而不是太少真是太好了。;)+1、这是最好的解决方案<代码>动态
和反射不是静态类型安全的——这是。就我个人而言,我更喜欢你的动态解决方案-闪亮:)哈哈,好吧,选择太多而不是太少真是太好了。;)