分組並賦值
public AllDesColumnEntity GetAllColumnList()
{
AllDesColumnEntity list = new AllDesColumnEntity();
string sql = @"SELECT function_id, property_valuename ‘字段編碼‘, property_displayname ‘字段名稱‘,* FROM BT_AllColumn WHERE status=0;";
var dt = SqlHelper.ExecuteDataset(dsDB_Read_ConnectionString, CommandType.Text, sql, null).Tables[0].AsEnumerable();
var query = (from t in dt.AsEnumerable()
group t by new { t1 = t.Field<Int32>("function_id") } into m
select new DataItem
{
functionId = m.Key.t1,
}).ToList();
foreach (var item in query)
{
var listChilds = dt.Where(x => x.Field<Int32>("function_id").Equals(item.functionId)).Select(x => new ChildItem() { Id = x.Field<Int32>("function_id"), 字段名稱 = x.Field<string>("字段名稱"), 字段編碼 = x.Field<string>("字段編碼") }).ToList();
if (listChilds.Count > 0)
{
item.Child = listChilds;
}
}
list.data = query;
list.total = query.Count;
return list;
}
public class ChildItem
{
/// <summary>
///
/// </summary>
public int Id { get; set; }
/// <summary>
/// 企業ID
/// </summary>
public string 字段名稱 { get; set; }
/// <summary>
///
/// </summary>
public string 字段編碼 { get; set; }
/// <summary>
///
/// </summary>
public string 是否選中 { get; set; }
}
public class DataItem
{
/// <summary>
///
/// </summary>
public int functionId { get; set; }
/// <summary>
/// 企業查詢
/// </summary>
public string 指標名稱 { get; set; }
/// <summary>
///
/// </summary>
public string 指標編碼 { get; set; }
/// <summary>
///
/// </summary>
public string 是否選中 { get; set; }
/// <summary>
///
/// </summary>
public string url { get; set; }
/// <summary>
///
/// </summary>
public List<ChildItem> Child { get; set; }
}
public class AllDesColumnEntity
{
/// <summary>
///
/// </summary>
public int id { get; set; }
/// <summary>
///
/// </summary>
public int total { get; set; }
/// <summary>
///
/// </summary>
public List<DataItem> data { get; set; }
/// <summary>
/// 查詢成功
/// </summary>
public string result { get; set; }
/// <summary>
///
/// </summary>
public string msg { get; set; }
}
分組並賦值