LINQ实现多条选择条件

时间:2015-04-22 05:35:59

标签: c# linq datatable

我有2个数据表。

        DataTable wdt = new DataTable();
        wdt.Columns.Add("wName", typeof(string));
        wdt.Columns.Add("userID1", typeof(string));
        wdt.Columns.Add("userID2", typeof(string));
        wdt.Columns.Add("userID3", typeof(string));
        wdt.Columns.Add("dttime", typeof(DateTime));

        DataTable mdt = new DataTable();
        mdt.Columns.Add("iD", typeof(string));
        mdt.Columns.Add("firstname", typeof(string));
        mdt.Columns.Add("lastname", typeof(string));

        DataTable dt = new DataTable();
        dt.Columns.Add("wName", typeof(string));
        dt.Columns.Add("user1", typeof(string));
        dt.Columns.Add("user2", typeof(string));
        dt.Columns.Add("user3", typeof(string));
        dt.Columns.Add("dttime", typeof(DateTime));

        for (int i = 0; i < wdt.Rows.Count; i++)
        {
            DataRow ndr = dt.NewRow();
            ndr[0] = wdt.Select()[i][0].ToString();

            ndr[1] = (from r in mdt.AsEnumerable()
                              where r.Field<int>("iD") == Convert.ToInt32(wdt.Rows[i][1])
                              select r.Field<string>("firstName") + " " + r.Field<string>("lastName")).First<string>();
                ndr[2] = (from r in mdt.AsEnumerable()
                          where r.Field<int>("iD") == Convert.ToInt32(wdt.Rows[i][2])
                          select r.Field<string>("firstName") + " " + r.Field<string>("lastName")).First<string>();
                ndr[3] = (from r in mdt.AsEnumerable()
                          where r.Field<int>("iD") == Convert.ToInt32(wdt.Rows[i][3])
                          select r.Field<string>("firstName") + " " + r.Field<string>("lastName")).First<string>();


           ndr[4] = wdt.Select()[i][4].ToString();

           dt.Rows.Add(ndr);
        }

在上面的代码中,我通过计算来自wdt&amp;的数据得到一个新的数据表dt。 MDT。但是在这里,我必须在循环中运行LINQ语法。有可能避免循环和放大在单个LINQ循环中要完成同样的工作吗?

Datatable1:

iD  firstname  lastname
1      b           v
2      d           c
3      f           g
4      s           o

....

Datatable2:

Code        userid1    userid2      userid3  work
    1f           1           3           6       gg
    2g           1           4           7       gg
    3b           3           4           7       gg
    4v           4           3           8       gg

预期的新数据表:

Code    username1   username2   username3  work
1f           a           b           c       gg
2g           d           f           r       gg
3b           c           h           g       gg
4v           d           s           h       gg

这里,来自datatable1&amp;的iD userID1,userID2,userID3相同。

1 个答案:

答案 0 :(得分:1)

username1你可以使用左连接进行查询并在飞行对象上返回新的拥有你需要的所有属性,如下所示

    var newData = (from a in wdt.AsEnumerable()
        join user1Info in mdt.AsEnumerable() on a["userID1"] equals user1Info["iD"] into lUser1Info
        join user2Info in mdt.AsEnumerable() on a["userID2"] equals user2Info["iD"] into lUser2Info
        join user3Info in mdt.AsEnumerable() on a["userID3"] equals user3Info["iD"] into lUser3Info
        from user1Info in lUser1Info.DefaultIfEmpty()
        from user2Info in lUser2Info.DefaultIfEmpty()
        from user3Info in lUser3Info.DefaultIfEmpty()
        select new
        {
            wName = a["wName"].ToString(),
            username1 = user1Info == null ? string.Empty : user1Info["firstname"].ToString() + user1Info["lastname"],
            username2 = user2Info == null ? string.Empty : user2Info["firstname"].ToString() + user2Info["lastname"],
            username3 = user3Info == null ? string.Empty : user3Info["firstname"].ToString() + user3Info["lastname"],
            dttime = a["dttime"]
        }).ToList();