在进行项目的过程,有一个需求是根据不同的权限,然后页面显示不同的内容,需前端在表格中显示的时候进行数据的拼接。需求的拼接要求如下:
, 然后,我采取的形式是,在后台取出数据,进行拼接;首先是将数据进行分组,然后制定数组,用特定的符号拼接好,然后循环放到数组中。代码如下:
//根据不同的要求进行分组的查询
var rolelistnum = rolelist.Where(p => p.PMES_USER_GROUP_ID.Contains("PKG_FUNCTION_NAME")).ToList();
var alllistnum = rolelist.Where(p => p.PMES_USER_GROUP_ID.Contains("PKG_FUNCTION_ALL")).ToList();
//制定数组
List<RECIPE> listView = new List<RECIPE>();
if (rolelistnum.Count > 0)
{
// 按照FUNDITION_CODE, PN,将其他的字段进行合并;
var group = list.GroupBy(g => new { g.FUNDITION_CODE, g.PN });
foreach (var item in group)
{
//设定需要合并字段的数组
List<string> dissolvantList = new List<string>();
List<string> dissolvantValueList = new List<string>();
List<string> dissolvantNameList = new List<string>();
List<string> additiveList = new List<string>();
List<string> additiveNameList = new List<string>();
List<string> additiveValueList = new List<string>();
List<string> lithiumlist = new List<string>();
List<string> lithiumValuelist = new List<string>();
List<string> lithiumNamelist = new List<string>();
//循环将相同的数据放入到数组中
foreach (var model in item)
{
dissolvantList.Add(model.DISSOLVANT); dissolvantValueList.Add(model.DISSOLVANT_NUM);
dissolvantNameList.Add(model.DISSOLVANTNAME);
additiveList.Add(model.ADDITIVE);
additiveValueList.Add(model.ADDITIVE_NUM);
additiveNameList.Add(model.ADDITIVENAME);
lithiumlist.Add(model.LITHIUM);
lithiumValuelist.Add(model.LITHIUM_NUM);
lithiumNamelist.Add(model.LITHIUMNAME);
}
//在数组中进行分割
var dissolvant_name = string.Join("/", dissolvantNameList) + "=" + string.Join("/", dissolvantValueList) ;
var additive_name = string.Join("/", additiveNameList) + "=" + string.Join("/", additiveValueList) ;
var lithium_name = string.Join("/", lithiumNamelist) + "=" + string.Join("/", lithiumValuelist) ;
//实体的赋值,返回到前台显示
listView.Add(new RECIPE
{
FUNDITION_CODE = item.Key.FUNDITION_CODE,
DISSOLVANT = dissolvant_name,
PN = item.Key.PN,
ADDITIVE = additive_name,
LITHIUM = lithium_name
});
}
}
最终的实现效果: