开发者

How do I call the Count method on an Entity Framework Context by using Reflection?

开发者 https://www.devze.com 2023-01-09 07:10 出处:网络
I\'m trying to get count of records of each table by using the following code. But it doesn\'t work at all. GetMethod(\"Count\") returns always null. I might do really stupid thing though, I don\'t kn

I'm trying to get count of records of each table by using the following code. But it doesn't work at all. GetMethod("Count") returns always null. I might do really stupid thing though, I don't know what to do.

var list = new List<Tabl开发者_JAVA技巧eInfoContainer>
{
    new TableInfoContainer{ObjectSetPropertyName="table1"},
    new TableInfoContainer{ObjectSetPropertyName="table2"},
    new TableInfoContainer{ObjectSetPropertyName="table3"},
};

using (var context = new DBEntities())
{
    list.ForEach(x =>{
        var property = context.GetType().GetProperty(x.ObjectSetPropertyName);
        x.RecordCount = (int)typeof(IQueryable).GetMethod("Count").Invoke(property, null);
    });
}

Aany help would be appreciated!

Thanks, yokyo

0

精彩评论

暂无评论...
验证码 换一张
取 消