通常在实际开发中,业务对象的信息是需要来自多个数据表的。
我们如果想要获取这个业务对象,就要先查询数据表,再把查询到的数据依次循环,组合转换封装成业务要使用的对象类型列表。
如果使用了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;
}