DataTable比较特殊的处理

qq_24567365 2018-09-18 10:39:04


现在有个比较特殊的需求需要处理,本人lambda学的差,不知道怎么处理,希望大神能提供代码解决下,是这样的,有个如图所示的DataTable,然后我模拟了一批数据,然后有一个方法有三个参数,当Source是3或者6的时候,Type列才有值,值要么为1,要么为2,然后又对应的ID列,(Source是3或者6这是一个特殊的情况)实际场景就是数据是从两个表取出来的,当Source为3或者为6时,当Type为1取得ID值就是A表的值,当Type为2的时候取的就是B表的值,所以才这样,然后根据Source将ID列合并为一行,用逗号拼接,我现在模拟了字典dic1,dic2,然后就是不知道怎么从DataTable通过lambda或者Linq实现到字典的这个过程,现在字典加的是模拟数据,实际肯定是从Datatable里取得,但是不知道怎么实现,想了很久都实现不了,希望大神能代码帮助下,我的C币就剩9个了,续费的话太贵了,还请大神们能指导下
 
DataTable da = new DataTable();
da.Columns.Add("ID", typeof(System.Int32));
da.Columns.Add("Source", typeof(System.Int32));
da.Columns.Add("Type", typeof(System.Int32));

DataRow dr = da.NewRow();
da.Rows.Add(1001, 1, null);
da.Rows.Add(1002, 1, null);
da.Rows.Add(1003, 1, null);
da.Rows.Add(1004, 2, null);
da.Rows.Add(1005, 2, null);
da.Rows.Add(1006, 3, 1);
da.Rows.Add(1007, 3, 2);
da.Rows.Add(1008, 3, 2);
da.Rows.Add(1009, 6, 1);
da.Rows.Add(1010, 6, 2);
da.Rows.Add(1011, 6, 1);

Dictionary<int, string> dic1 = new Dictionary<int, string>();
dic1.Add(1,"1001,1002,1003");
dic1.Add(2,"1004,1005");

Dictionary<int, Dictionary<int,string> > dic2 = new Dictionary<int, Dictionary<int, string>>();

Dictionary<int, string> dic3 = new Dictionary<int, string>();
Dictionary<int, string> dic4 = new Dictionary<int, string>();
dic3.Add(1, "1006");
dic3.Add(2, "1007,1008");

dic4.Add(1, "1009,1011");
dic4.Add(2, "1010");

dic2.Add(3, dic3);
dic2.Add(6, dic4);

foreach (KeyValuePair<int,string> item in dic1)
{
Test(item.Key, item.Value, 0);
}
foreach (KeyValuePair<int, Dictionary<int, string>> item in dic2)
{
foreach(KeyValuePair<int, string> temp in item.Value)
{
Test(item.Key, temp.Value, temp.Key);
}
}
public void Test(int source,string id,int type)
{

}
...全文
319 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_24567365 2018-09-26
  • 打赏
  • 举报
回复
测试代码不能运行?
qq_24567365 2018-09-19
  • 打赏
  • 举报
回复
熬夜稍微搞了下,但是代码看上去太low了,也没时间优化,太困了,有没有大神有更好的办法

DataTable da = new DataTable();
da.Columns.Add("ID", typeof(System.Int32));
da.Columns.Add("Source", typeof(System.Int32));
da.Columns.Add("Type", typeof(System.Int32));

DataRow dr = da.NewRow();
da.Rows.Add(1001, 1, null);
da.Rows.Add(1002, 1, null);
da.Rows.Add(1003, 1, null);
da.Rows.Add(1004, 2, null);
da.Rows.Add(1005, 2, null);
da.Rows.Add(1012, 7, null);
da.Rows.Add(1013, 7, null);
da.Rows.Add(1006, 3, 1);
da.Rows.Add(1007, 3, 2);
da.Rows.Add(1008, 3, 2);
da.Rows.Add(1009, 6, 1);
da.Rows.Add(1010, 6, 2);
da.Rows.Add(1011, 6, 1);

Dictionary<int, string> dic1 = new Dictionary<int, string>();
Dictionary<int, Dictionary<int, string>> dic2 = new Dictionary<int, Dictionary<int, string>>();

Dictionary<int, string> dic3 = new Dictionary<int, string>();
Dictionary<int, string> dic4 = new Dictionary<int, string>();
var dd=da.AsEnumerable().Where(x => Convert.ToInt32(x["Source"]) != 3 && Convert.ToInt32(x["Source"]) != 6).GroupBy(p => new { ID = p["ID"], Source = p["Source"], Type = p["Type"] });
var dd2 = da.AsEnumerable().Where(x => Convert.ToInt32(x["Source"]) == 3 || Convert.ToInt32(x["Source"]) == 6).GroupBy(p => new { ID = p["ID"], Source = p["Source"], Type = p["Type"] });
string id1 = string.Empty;
string id2 = string.Empty;
string id4 = string.Empty;
string id7 = string.Empty;
string id31 = string.Empty;
string id32 = string.Empty;
string id61 = string.Empty;
string id62 = string.Empty;
int count1=dd.Where(x => Convert.ToInt32(x.Key.Source) == 1).Count();
int count2 = dd.Where(x => Convert.ToInt32(x.Key.Source) == 2).Count();
int count4 = dd.Where(x => Convert.ToInt32(x.Key.Source) == 4).Count();
int count7 = dd.Where(x => Convert.ToInt32(x.Key.Source) == 7).Count();

int count31= dd2.Where(x => Convert.ToInt32(x.Key.Source) == 3&& Convert.ToInt32(x.Key.Type)==1).Count();
int count32 = dd2.Where(x => Convert.ToInt32(x.Key.Source) == 3 && Convert.ToInt32(x.Key.Type) == 2).Count();
int count61= dd2.Where(x => Convert.ToInt32(x.Key.Source) == 6 && Convert.ToInt32(x.Key.Type) == 1).Count();
int count62 = dd2.Where(x => Convert.ToInt32(x.Key.Source) == 6 && Convert.ToInt32(x.Key.Type) == 2).Count();
int num1 = 0;
int num2 = 0;
int num4 = 0;
int num7 = 0;
int num31 = 0;
int num32 = 0;
int num61 = 0;
int num62 = 0;
foreach (var g in dd)
{

if (Convert.ToInt32(g.Key.Source) == 1)
{
id1 += g.Key.ID + ",";
num1++;
}
if (id1.Length > 0&&num1==count1)
{
dic1.Add(Convert.ToInt32(g.Key.Source), id1.Trim(','));
num1 = 0;
}
if (Convert.ToInt32(g.Key.Source) == 2)
{
id2 += g.Key.ID + ",";
num2++;
}
if (id2.Length > 0 && num2 == count2)
{
dic1.Add(Convert.ToInt32(g.Key.Source), id2.Trim(','));
num2 = 0;
}
if (Convert.ToInt32(g.Key.Source) == 4)
{
id4 += g.Key.ID + ",";
num4++;
}
if (id4.Length > 0 && num4 == count4)
{
dic1.Add(Convert.ToInt32(g.Key.Source), id4.Trim(','));
num4 = 0;
}
if (Convert.ToInt32(g.Key.Source) == 7)
{
id7 += g.Key.ID + ",";
num7++;
}
if (id7.Length > 0 && num7 == count7)
{
dic1.Add(Convert.ToInt32(g.Key.Source), id7.Trim(','));
}


}

foreach(var k in dd2)
{
if (Convert.ToInt32(k.Key.Source) == 3)
{
if(Convert.ToInt32(k.Key.Type) == 1)
{
id31 += k.Key.ID + ",";
num31++;
}
else if (Convert.ToInt32(k.Key.Type) == 2)
{
id32 += k.Key.ID + ",";
num32++;
}

}
if (id31.Length > 0 && num31 == count31&& Convert.ToInt32(k.Key.Source) == 3 && Convert.ToInt32(k.Key.Type)==1)
{
dic3.Add(1, id31.Trim(','));
}
if (id32.Length > 0 && num32 == count32 && Convert.ToInt32(k.Key.Source) == 3 && Convert.ToInt32(k.Key.Type) == 2)
{
dic3.Add(2, id32.Trim(','));
}
if (Convert.ToInt32(k.Key.Source) == 6)
{
if (Convert.ToInt32(k.Key.Type) == 1)
{
id61 += k.Key.ID + ",";
num61++;
}
else if(Convert.ToInt32(k.Key.Type) == 2)
{
id62 += k.Key.ID + ",";
num62++;
}

}
if (id61.Length > 0 && num61 == count61 && Convert.ToInt32(k.Key.Source) == 6 && Convert.ToInt32(k.Key.Type) == 1)
{
dic4.Add(1, id61.Trim(','));
}
if (id62.Length > 0 && num62 == count62 && Convert.ToInt32(k.Key.Source) == 6 && Convert.ToInt32(k.Key.Type) == 2)
{
dic4.Add(2, id62.Trim(','));
}
}

if(dic3.Count()>0)
{
dic2.Add(3, dic3);
}
if (dic4.Count() > 0)
{
dic2.Add(6, dic4);
}


foreach (KeyValuePair<int,string> item in dic1)
{
Test(item.Key, item.Value, 0);
}
foreach (KeyValuePair<int, Dictionary<int, string>> item in dic2)
{
foreach(KeyValuePair<int, string> temp in item.Value)
{
Test(item.Key, temp.Value, temp.Key);
}
}
xuzuning 2018-09-19
  • 打赏
  • 举报
回复
你花了很多心思做了很多说明,但并未说道要点上
比如你说有 A表、B表,但模拟数据只有一个 DataTable,如果是合并后的数据,那么再提及 A表、B表 和 Type 那只能混淆自己的思路,而作茧自缚
你贴出了测试代码,但并不能运行,所以也很难推测出你需要的结果
姑且猜一把
            DataTable da = new DataTable();
da.Columns.Add("ID", typeof(System.Int32));
da.Columns.Add("Source", typeof(System.Int32));
da.Columns.Add("Type", typeof(System.Int32));

DataRow dr = da.NewRow();
da.Rows.Add(1001, 1, null);
da.Rows.Add(1002, 1, null);
da.Rows.Add(1003, 1, null);
da.Rows.Add(1004, 2, null);
da.Rows.Add(1005, 2, null);
da.Rows.Add(1012, 7, null);
da.Rows.Add(1013, 7, null);
da.Rows.Add(1006, 3, 1);
da.Rows.Add(1007, 3, 2);
da.Rows.Add(1008, 3, 2);
da.Rows.Add(1009, 6, 1);
da.Rows.Add(1010, 6, 2);
da.Rows.Add(1011, 6, 1);

var a = da.AsEnumerable().Where(x =>(int)x["Source"] == 3).Select(x=>new {Id=(int)x["Id"], Source=(int)x["Source"],Type=(int)x["Type"]});
foreach (var x in a) Console.WriteLine(x);
{ Id = 1006, Source = 3, Type = 1 }
{ Id = 1007, Source = 3, Type = 2 }
{ Id = 1008, Source = 3, Type = 2 }

再 Console.WriteLine(string.Join(",", a.Select(x=>x.Id)));
得 1006,1007,1008
其实,你只需给出原始的数据和期望的结果,自然就会有人给你算法的
本课程是PowerBI系列课程之DAX函数专题讲解,包含以下内容 1.  DAX函数基础知识什么是DAX函数数学函数:ABS、DIVIDE、MOD、RAND、ROUND、FIXED等日期和时间函数: CALENDAR、CALENDARAUTO、MONTH、YEAR、DATE、DT等信息函数:USERNAME、USERPRINCIPALNAME、HASONEFILTER、HASONEVALUE、ISFILTERED、ISCROSSFILTERED、ISINSCOPE、ISBLANK、SELECTEDMEASURE、SELECTEDMEASURENAME等逻辑函数:AND、OR、IF、IFERROR、SWITCH、TRUE、FALSE、COALESCE(官方文档含糊不清-结合实例)等关系函数:CROSSFILTER、RELATED、RELATEDTABLE等筛选器函数:FILTER、CALCULATE、ALL、ALLEXCEPT、ALLSELECTED、EARLIER、KEEPFILTERS、REMOVEFILTERS、SELECTEDVALUE、LOOKUPVALUE等父子函数:PATH、PATHCONTAINS、PATHITEM、PATHLENGTH等统计函数:AVERAGE、COUNT、MAX、MIN、SUM等迭代统计函数:AVERAGEX、COUNTX、MAXX、MINX、SUMX、RANKX等表函数: FILTERS 、ADDCOLUMNS、 SELECTCOLUMNS、 CROSSJOIN、 EXCEPT、 GENERATE、 GROUPBY、 SUMMARIZE、 SUMMARIZECOLUMNS、 TOPN、 TREATAS、 UNION、 VALUES、DISTINCT、DATATABLE、NATUALINNERJOIN、NATRUALLEFTOUTERJOIN等文本函数: EXACT、MID、 FIND、 LEN、 REPT、 LOWER、 UPPER、 UNICHAR等时间智能函数:DATEADD、DATESMTD、FIRSTDATE、LASTDATE、SAMEPERIODLASTYEAR等财务函数:2020.7之后发布的,和Excel中财务函数相似,网页和demo pbix简单介绍其他函数:BLANK、ERROR、IFERROR等 DAX函数初体验:Max、Sum、Divide、if、Values等值函数表函数以及表和列的概念DAX函数术语、语法、运算符DAX运算符和引擎中字母大小写问题DAX编程注释和快捷键DAX与Excel函数的共同点和区别(PPT)DAX、xmSQL与SQL表达式的区别(PPT)DAX函数的自学途径 2.  PowerBI中数据建模知识维度建模关系传递和交叉筛选器方向-理解表关系(1v1, 1vM, Mv1,MvM)两个方向上应用安全筛选器关闭关系自动检测新建计算列新建度量值新建计算表:辅助表(五种方式)、日历表数据类型讲解数据格式控制:%、$、千位分隔符、小数位、日期格式Format函数自定义数据格式Convert函数做数据类型转换解决中文数字单位 万 的显示问题Date和DT函数定义固定日期值显示和隐藏列DAX代码分析器阅读DAX表达式方法:从上至下、由内到外(注意Calculate的计算顺序)调试DAX表达式方法:分布输出或VAR输出3.  DAX函数原理 Vertipaq列式数据库原理理解度量值和计算列理解行上下文和筛选上下文:Calculate示意图行上下文中使用VAR替代EARLIERVAR变量在定义时的上下文中计算VAR变量是采用惰性计算(使用时计算)理解扩展表和RELATED函数理解数据沿袭Lineage 4.  开始感知DAX函数的强大DAX函数实现特殊符号的使用DAX函数实现切片器默认当前月或天DAX函数使切片器默认代表无任何选择DAX函数使切片器仅显示有数据的选项DAX函数使切片器反向筛选和计算DAX函数使切片器之间取并集DAX函数使关系中多端的切片器筛选一端的切片器 DAX函数实现年月共同决定数据排序DAX函数实现动态图表标题DAX函数实现动态图表配色和图标DAX函数实现动态纵坐标DAX函数实现动态横坐标5.  理解重点DAX函数重中之重FILTER 和 CALCULATE和CALCULATETABLE详解调节器REMOVEFILTERS和ALL、ALLEXCEPT函数调节器ALL、ALLSELECTED和ISINSCOPE占比分析调节器AllSELECTED和KEEPFILTERS的比较调节器USERELATIONSHIP激活关系调节器TREATAS动态建立关系调节器CROSSFILTER改变筛选器方向重点之ISFILTERED和ISCROSSFILTERED重点之HASONEVALUE和ISINSCOPE的区别重点之表函数SELECTEDCOLUMNS和ADDCOLUMNS重点之表函数NATUALINNERJOIN和NATRUALLEFTOUTERJOIN重点之表函数FILTERS和VALUES比较重点之VALUES和DISTINCT的区别重点之分组函数SUMMARIZECOLUMNS详解重点之函数LOOKUPVALUE vs RELATED vs VLOOKUP 重点之集合函数UNION、INTERSECT、EXCEPT重点之集合函数CROSSJOIN和GENERATE 笛卡尔积重点之值合并、列合并、表合并CONCATENATEX重点之BLANK行产生的原因和BLANK相关函数重点之COALESCE函数处理空重点之FIRSTNOBLANK和FIRSTNOBLANKVALUE函数重点之使用VAR变量表中的列重点之Error和IfError函数6.  实际案例-日期时间和时间智能相关关键点-日期表和事实表关联问题时间智能-同比环比分析时间智能-累计聚合、滚动聚合、移动平均时间智能-期初期末库存分析日期分析-计算任意所选月份的环比日期分析-周的同比环比和周聚合日期分析-指定月份的同比环比和季度环比日期分析-计算季末或季末月份的数据日期分析-趋势图中根据最近月份取TopN日期分析-动态指定某个日期区间分析日期分析-动态任意区间段做数据对比日期分析-实现两个日期列的范围筛选日期分析-按工作日计算日期差日期分析-计算最近两次购买日期差日期分析-根据历史数据做销售预测日期时间函数和时间智能函数使用总结7.  实际案例-DAX函数进阶进阶-解决列排序对计算的影响进阶-实现切片器筛选之间的OR逻辑进阶-矩阵Matrix中高亮显示最大值最小值进阶-DAX列转行 vs 矩阵列转行和逆透视进阶-非日期类型的累计聚合进阶-排名逻辑的4种实现-RANKX详解进阶-分组内排名的实现和理解迭代函数进阶-TopN/BottomN和Others的实现进阶-TopN中实现动态指标进阶-TopN中实现N的动态进阶-分组内动态TopN和Others 进阶-商品折上折-迭代函数SUMX详解 进阶-分析客户购买行为进阶-找出无购买行为的客户进阶-客户购买商品关联度分析 进阶-新客户分析进阶-流失客户分析进阶-回流客户分析进阶-客户购买频次和区间分析进阶-RFM客户价值分析进阶-帕累托分析进阶-盈亏平衡分析报表性能优化思路(PPT)  

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧