110,533
社区成员
发帖
与我相关
我的任务
分享
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)
{
}
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);
}
}
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 }