1
https://stackoverflow.com/a/27205784/7468628に基づいて動的linqを使用してSelectExceptを作成しました。Select選択したテーブルを削除しない限り
コード:
public static IQueryable SelectExcept<TSource, TResult>(this IQueryable<TSource> source, List<string> excludeProperties)
{
var sourceType = typeof(TSource);
var allowedSelectTypes = new Type[] { typeof(string), typeof(ValueType) };
var sourceProperties = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => allowedSelectTypes.Any(t => t.IsAssignableFrom(((PropertyInfo)p).PropertyType))).Select(p => ((MemberInfo)p).Name);
var sourceFields = sourceType.GetFields(BindingFlags.Public | BindingFlags.Instance).Where(f => allowedSelectTypes.Any(t => t.IsAssignableFrom(((FieldInfo)f).FieldType))).Select(f => ((MemberInfo)f).Name);
var selectFields = sourceProperties.Concat(sourceFields).Where(p => !excludeProperties.Contains(p)).ToArray();
var dynamicSelect =
string.Format("new({0})",
string.Join(", ", selectFields));
return selectFields.Count() > 0
? source.Select(dynamicSelect)
: Enumerable.Empty<TSource>().AsQueryable<TSource>();
}
これは、フィールドで素晴らしい作品が、瞬間から、私のオブジェクトは、オブジェクトがテーブルからのものであり、SelectExceptを使用しているとき、私は参加した値を失い、他のテーブルと結合されます。この結合された価値をどうやって維持することができますか?