mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-08-23 22:11:36 +08:00
Support Mapper Many to Many
This commit is contained in:
parent
d3d5a1fdd4
commit
f7d8e5a0d1
@ -98,9 +98,9 @@ namespace OrmTest
|
||||
|
||||
.ToList();
|
||||
|
||||
//var list8 = Db.Queryable<A>()
|
||||
//.Mapper<ABMapping>(it=>ManyToMany.Config(it.A,it.AId,it.B,it.BId))
|
||||
//.ToList();
|
||||
var list8 = Db.Queryable<A>()
|
||||
.Mapper<A,B,ABMapping>(it => ManyToMany.Config(it.AId,it.BId))
|
||||
.ToList();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -109,10 +109,91 @@ namespace SqlSugar
|
||||
this.MapperAction.Add(mapperAction);
|
||||
return this;
|
||||
}
|
||||
public ISugarQueryable<T> Mapper<MappingType>(Expression<Func<MappingType, ManyToMany>> expression)
|
||||
public ISugarQueryable<T> Mapper<AType, BType, MappingType>(Expression<Func<MappingType, ManyToMany>> expression)
|
||||
{
|
||||
var args = ((expression as LambdaExpression).Body as MethodCallExpression).Arguments;
|
||||
|
||||
Type aType = typeof(AType);
|
||||
Type bType = typeof(BType);
|
||||
Type bListType = typeof(List<BType>);
|
||||
|
||||
this.Context.InitMappingInfo(aType);
|
||||
this.Context.InitMappingInfo(bType);
|
||||
this.Context.InitMappingInfo(typeof(MappingType));
|
||||
|
||||
//Mapping
|
||||
var mappingEntity = this.Context.EntityMaintenance.GetEntityInfo(typeof(MappingType));
|
||||
string m_aPropertyName = (args[0] as MemberExpression).Member.Name;
|
||||
string m_bPropertyName= (args[1] as MemberExpression).Member.Name;
|
||||
var m_aDbField = mappingEntity.Columns.First(it => it.PropertyName == m_aPropertyName).DbColumnName;
|
||||
var m_bDbField = mappingEntity.Columns.First(it => it.PropertyName == m_bPropertyName).DbColumnName;
|
||||
|
||||
//A
|
||||
var aEntity = this.Context.EntityMaintenance.GetEntityInfo(aType);
|
||||
var aPropertyName = aEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true).PropertyName;
|
||||
|
||||
//B
|
||||
var bEntity = this.Context.EntityMaintenance.GetEntityInfo(bType);
|
||||
var bProperty = bEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true).PropertyName;
|
||||
var bDbFiled = bEntity.Columns.FirstOrDefault(it => it.IsPrimarykey == true).DbColumnName;
|
||||
this.Mapper((it,cache) =>
|
||||
{
|
||||
var list= cache.Get<Dictionary<object, List<BType>>>(oldList=> {
|
||||
|
||||
|
||||
//query mapping by a
|
||||
var cons = new List<IConditionalModel>() {
|
||||
new ConditionalModel(){
|
||||
ConditionalType=ConditionalType.In,
|
||||
FieldName= m_aDbField,
|
||||
FieldValue=string.Join(",",oldList.Select(z=>UtilMethods.GetPropertyValue(z,aPropertyName)).Distinct())
|
||||
}
|
||||
};
|
||||
var mappingList = this.Context.Queryable<MappingType>().Where(cons).ToList();
|
||||
var bids = mappingList.Select(z => UtilMethods.GetPropertyValue(z, m_bPropertyName)).Distinct().ToList();
|
||||
|
||||
//queryable b by mapping
|
||||
cons = new List<IConditionalModel>() {
|
||||
new ConditionalModel(){
|
||||
ConditionalType=ConditionalType.In,
|
||||
FieldName= bDbFiled,
|
||||
FieldValue=string.Join(",",mappingList.Select(z=>UtilMethods.GetPropertyValue(z,m_bPropertyName)).Distinct())
|
||||
}
|
||||
};
|
||||
var bList = this.Context.Queryable<BType>().Where(cons).ToList();
|
||||
|
||||
//get result
|
||||
Dictionary<object, List<BType>> result = new Dictionary<object, List<BType>>();
|
||||
var group = mappingList.GroupBy(z => UtilMethods.GetPropertyValue(z, m_aPropertyName));
|
||||
foreach (var item in group)
|
||||
{
|
||||
var currentBids = item.Select(z => UtilMethods.GetPropertyValue(z, m_bPropertyName)).ToList();
|
||||
result.Add(item.Key, bList.Where(z => currentBids.Contains(UtilMethods.GetPropertyValue(z, bProperty))).ToList());
|
||||
}
|
||||
return result;
|
||||
|
||||
}, expression.ToString());
|
||||
foreach (var item in aEntity.Columns)
|
||||
{
|
||||
var aid = UtilMethods.GetPropertyValue(it, aPropertyName);
|
||||
if (list.ContainsKey(aid))
|
||||
{
|
||||
if (item.PropertyInfo.PropertyType == bType)
|
||||
{
|
||||
var b=UtilMethods.ChangeType<BType>(list[aid].FirstOrDefault());
|
||||
item.PropertyInfo.SetValue(it, b);
|
||||
}
|
||||
else if (item.PropertyInfo.PropertyType == bListType)
|
||||
{
|
||||
var bList = UtilMethods.ChangeType<List<BType>>(list[aid]);
|
||||
item.PropertyInfo.SetValue(it, bList);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
public virtual ISugarQueryable<T> Mapper(Action<T, MapperCache<T>> mapperAction)
|
||||
{
|
||||
this.MapperActionWithCache = mapperAction;
|
||||
|
@ -8,11 +8,7 @@ namespace SqlSugar
|
||||
{
|
||||
public class ManyToMany
|
||||
{
|
||||
public static ManyToMany Config<AClass,AField,BClass,BField>(AClass aClass,AField aField,BClass bClass,BField bField)
|
||||
where AClass:class
|
||||
where AField : struct
|
||||
where BClass:class
|
||||
where BField:struct
|
||||
public static ManyToMany Config<AField,BField>(AField aField,BField bField)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
@ -34,6 +34,21 @@ namespace SqlSugar
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public Result Get<Result>(Func<List<T>, Result> action,string cachekey)
|
||||
{
|
||||
GetIndex++;
|
||||
string key = "Get" + typeof(Result) + action.GetHashCode() + action.Method.Name+ cachekey;
|
||||
if (caches.ContainsKey(key))
|
||||
{
|
||||
return (Result)caches[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
var result = action(_list);
|
||||
caches.Add(key, result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
public List<Result> GetListByPrimaryKeys<Result>(Func<T, double?> action) where Result : class, new()
|
||||
{
|
||||
{
|
||||
|
@ -20,7 +20,7 @@ namespace SqlSugar
|
||||
ISugarQueryable<T> With(string withString);
|
||||
ISugarQueryable<T> Filter(string FilterName, bool isDisabledGobalFilter = false);
|
||||
ISugarQueryable<T> Mapper(Action<T> mapperAction);
|
||||
ISugarQueryable<T> Mapper<MappingType>(Expression<Func<MappingType, ManyToMany>> expression);
|
||||
ISugarQueryable<T> Mapper<AType, BType, MappingType>(Expression<Func<MappingType, ManyToMany>> expression);
|
||||
ISugarQueryable<T> Mapper(Action<T, MapperCache<T>> mapperAction);
|
||||
ISugarQueryable<T> Mapper<TObject>(Expression<Func<T, TObject>> mapperObject, Expression<Func<T, object>> mainField, Expression<Func<T, object>> childField);
|
||||
ISugarQueryable<T> Mapper<TObject>(Expression<Func<T, List<TObject>>> mapperObject, Expression<Func<T, object>> mainField, Expression<Func<T, object>> childField);
|
||||
|
@ -244,7 +244,10 @@ namespace SqlSugar
|
||||
{
|
||||
return Convert.ToInt64(string.Join("", bytes).PadRight(20, '0'));
|
||||
}
|
||||
|
||||
public static object GetPropertyValue<T>(T t, string PropertyName)
|
||||
{
|
||||
return t.GetType().GetProperty(PropertyName).GetValue(t, null);
|
||||
}
|
||||
internal static string GetMD5(string myString)
|
||||
{
|
||||
MD5 md5 = new MD5CryptoServiceProvider();
|
||||
|
Loading…
Reference in New Issue
Block a user