C# 将泛型类型与实体框架结合使用&;ADO.NET模型
有没有一种方法可以在实体框架中使用通用方法 例如,我有表:C# 将泛型类型与实体框架结合使用&;ADO.NET模型,c#,asp.net,generics,entity-framework-4,C#,Asp.net,Generics,Entity Framework 4,有没有一种方法可以在实体框架中使用通用方法 例如,我有表:TblPlan和TblTier。我可以在泛型方法中传递它们并找到类型吗?每个表都有稍微不同的值,我想比较每个表的不同值。我试过: public static Dictionary<T, List<T>> checkDuplicates<T>(T something) { try { //TblCommissionPlan plan = (TblCommis
TblPlan
和TblTier
。我可以在泛型方法中传递它们并找到类型吗?每个表都有稍微不同的值,我想比较每个表的不同值。我试过:
public static Dictionary<T, List<T>> checkDuplicates<T>(T something)
{
try
{
//TblCommissionPlan plan = (TblCommissionPlan)something;
//var type = typeof(something);
}
}
公共静态字典检查重复项(T)
{
尝试
{
//TblCommissionPlan=(TblCommissionPlan)某物;
//变量类型=类型(某物);
}
}
这些不起作用……有什么想法吗?这里有一个类,我用来显示EF生成的单个实体的属性 您可以在此基础上比较两个不同的实体,这应该是一个很好的起点
namespace Solutions.Data.Entities
{
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Text;
using System.Linq;
public static class EntityExtensions
{
#region Fields
private static readonly ConcurrentDictionary<string, PropertyInfo[]> PropertyInfoCache = new ConcurrentDictionary<string, PropertyInfo[]>();
#endregion
#region Extension Methods
/// <summary>
/// This method will find all the Properties of Entity and display them in formatted way.
/// </summary>
/// <typeparam name="T">Entity Type</typeparam>
/// <param name="value">Entity value</param>
/// <returns>Formatted string of the Entity Properties</returns>
public static string PropertiesToString<T>(this T value) where T : IObjectWithChangeTracker
{
var type = typeof(T).FullName;
if (String.IsNullOrEmpty(type)) return String.Empty;
CachePropertyInfo<T>(type);
StringBuilder stringBuilder = new StringBuilder();
foreach (var propertyInfo in PropertyInfoCache[type])
{
stringBuilder.AppendLine(String.Format("{0} : {1}", propertyInfo.Name, propertyInfo.GetValue(value, null)));
}
return stringBuilder.ToString();
}
/// <summary>
/// Use reflection to find all propertied if key is not found in list.
/// </summary>
/// <typeparam name="T">Entity Type</typeparam>
/// <param name="type">property fullname</param>
private static void CachePropertyInfo<T>(string type)
{
if (!PropertyInfoCache.ContainsKey(type))
{
// Get all public properties of T type where T inherits interface IObjectWithChangeTracker
var properties =
typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.Name != "ChangeTracker");
PropertyInfoCache[type] = properties.ToArray();
}
}
#endregion
}
}
namespace Solutions.Data.Entities
{
使用制度;
使用System.Collections.Concurrent;
运用系统反思;
使用系统文本;
使用System.Linq;
公共静态类EntityExtensions
{
#区域字段
私有静态只读ConcurrentDictionary属性inFoccache=new ConcurrentDictionary();
#端区
#区域扩展方法
///
///此方法将查找实体的所有属性并以格式化方式显示它们。
///
///实体类型
///实体价值
///实体属性的格式化字符串
公共静态字符串属性字符串(此T值),其中T:IObjectWithChangeTracker
{
var type=typeof(T).全名;
if(String.IsNullOrEmpty(type))返回String.Empty;
CachePropertyInfo(类型);
StringBuilder StringBuilder=新的StringBuilder();
foreach(PropertyInfoCache[type]中的var propertyInfo)
{
AppendLine(String.Format(“{0}:{1}”,propertyInfo.Name,propertyInfo.GetValue(value,null));
}
返回stringBuilder.ToString();
}
///
///若在列表中找不到键,则使用反射查找所有属性。
///
///实体类型
///属性全名
私有静态void CachePropertyInfo(字符串类型)
{
如果(!PropertyInfoCache.ContainsKey(类型))
{
//获取T类型的所有公共属性,其中T继承接口IObjectWithChangeTracker
var特性=
typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p=>p.Name!=“ChangeTracker”);
PropertyInfoCache[type]=properties.ToArray();
}
}
#端区
}
}
这不是泛型方法的目的。如果您编写的方法具有签名:
public Dictionary<T, List<T>> CheckDuplicates<T>(T something)
现在您知道,传递给方法的每个T
都必须实现ISomeInterface
,并且您可以在方法中使用该接口上声明的任何属性或方法
对于不同类型的T
,方法的内容不应该不同,但逻辑可以不同,因为您可以调用T
s方法和属性,这些方法和属性可以有不同的实现。如果还不够,您可以传递另一个参数-generic delegate或基于T
的另一个泛型类,这将为您添加一些额外的逻辑
在您的场景中,您希望以不同的方式比较每个传递的类=>比较不能是方法的一部分,但它必须是实体的一部分,或者必须传递将对方法进行比较的其他类/方法
为了直接在类中实现比较,可以实现IComparable
接口,并将方法声明为:
public Dictionary<T, List<T>> CheckDuplicates<T>(T something) where T : IComparable<T>
公共字典检查重复项(T某物),其中T:i可比较
要在类之外实现比较,只需使用Func
或IComparer
的实现:
public Dictionary检查重复项(T something,i比较程序)
在任何一种情况下,我都不确定这与实体框架有什么关系,因为您方法的签名与EF无关。当您的意思是检查不同的值时,您试图检查哪些值?例如,如果我有
Plan.nm==Plan2.nm
,那么您想检查EF生成的计划的属性吗?
public Dictionary<T, List<T>> CheckDuplicates<T>(T something) where T : IComparable<T>
public Dictionary<T, List<T>> CheckDuplicates<T>(T something, IComparer<T> comparer)