0
我需要從屬性列表中找到單個屬性,該屬性與基於聲明類型的類型生成的另一個列表中的字符串相匹配。選擇發生在兩個列表中的單個元素
╔═════════════╦═════════════════╗
║ Properties ║ Names ║
╠═════════════╬═════════════════╣
║ User.UserId ║ Id ║
║ User.Name ║ IdUser ║
║ User.Age ║ UserId <- MATCH ║
║ User.Email ║ Key ║
║ ║ UserKey ║
║ ║ KeyUser ║
╚═════════════╩═════════════════╝
private const string IdAffix = "Id";
private const string KeyAffix = "Key";
private static readonly Func<Type, IEnumerable<string>> GetIdentityNamePatterns
= type => new [] { IdAffix, KeyAffix, type.Name + IdAffix, IdAffix + Type.Name,...}
private Expression<Func<TEntity, TIdentity> KeySelector = entity =>
{
var type = typeof(TEntity);
if(type.IsAssignableFrom(typeof(IEntity<TIdentity>)))
return ((IEntity<TIdentity>) entity).Id;
const BindingFlags bindingFlags
= BindingFlags.IgnoreCase | BindingFlags.GetProperty | BindingFlags.Public;
var properties = type.GetProperties(bindingFlags);
var identityProperty = entityProperties.SingleOrDefault(x =>
Attribute.IsDefined(x, typeof(IdentityAttribute)) && x.PropertyType == typeof (TIdentity))
// this is where I'm stuck
// how to loopthe list of possible names while looping
// through the properties at the same time?
?? entityProperties.SingleOrDefault(
x => x.Name.Equals... // GetIdentityNamePatterns(type)
[相交](http://msdn.microsoft.com/en-us/library/vstudio/bb460136(V = VS.100)的.aspx)可能對你 – DGibbs 2014-09-19 15:49:34
所以有用 - 基本上要返回某個對象的屬性 - 該屬性的名稱存在於其他列表中?這就是全部? – 2014-09-19 15:58:39
看起來像OP想從左邊選擇'User.UserId'時在右邊的列表中找到'UserId'我會查看.Contains()方法或IndexOf(「。」)以獲取所有內容「'。」'的權利將返回要在右列表中搜索的鍵值[列表 .Contains()](http://msdn.microsoft.com/en-us/library/bhkz42b3(v = vs.110).aspx) –
MethodMan
2014-09-19 16:01:07