??????? 通常在實際開發中,業務對象的信息是需要來自多個數據表的。
??????? 我們如果想要獲取這個業務對象,就要先查詢數據表,再把查詢到的數據依次循環,組合轉換封裝成業務要使用的對象類型列表。
??????? 如果使用了ORM,那么這個過程就可以簡化。示例代碼如下:
public boo1 GetManyApiEntities( out string error
, Dictionary< Guid, List<Guid>> dic
, out List<API_EntityInfo> Info
, out List<Tuple<Guid, Guid>> errDevRelated
, out List<Tuple<Guid, Guid>> errVars)
{ error=string.Empty;var sugarDb = new SqlSugarScope(){//...數據庫連接初始化};Info = new List<API_EntityInfo>();errDevRelated = nul1;errVars = null;try{foreach (var item in dic){ //list對象轉字典var tempDlls = sugarDb. Queryable<EDll>().Where(it => it.TestModel == item. Key && iten. Value.Contains(it. EGuid)).ToList ().GroupBy (it => it. EGuid).ToList ().ToDictionary(g => g.Key, g => g.ToList ());//查詢數據表,并轉換成對象var tempObj = sugarDb. Queryable<EntityInfo>().LeftJoin<Vars>((s, v) => iten.Key == v. TestMlodel && v. VarsInfoGuid == s.DefaultVarsGuid).LeftJoin(Related>((s, v, r) => r.TestModel == item. Key && r.RelateInfoGuid == s.DefaultDevRelateGuid).Where(s => s. TestMlodel == item.Key && item.Value.Contains(s. EGuid)).Select((s, v, r) =) new API_EntityInfo(){ TestModelID = s. TestModel,EntityID = s. EGuid,Entity = s,Related = r, Vars= v}).ToList();if(tempD11s.Keys.Count >0){foreach (var kv in tempDlls){tempObj.Find(x => x. EGuid == kv.Key).SeqActionInfos = kv.Value;}}Info. AddRange (tempObj);} if(Info.Count >0){ errDevRelated = Info.FindAll(x => x.SeqRelate == nul1). Select (x => new Tuple(Guid, Guid> (x. EGuid, z.TestModelID)).ToList ();errVars = Info.FindAll(x =>x.SeqVars = nul1).Select(x => new Tuple(Guid, Guid> (x. EGuid, z. TestModelID)).ToList ();return true;}}catch (System.Exception ex){error=ex.Message;} return false;
}