日期:2014-05-20  浏览次数:21012 次

下面的linq求vb些写法

 static void Main(string[] args)
        {

            DataTable dtA = new DataTable();
            dtA.Columns.Add("id", typeof(int));
            dtA.Columns.Add("price", typeof(string));
            dtA.Rows.Add(1, "100");
            dtA.Rows.Add(2, "100");

            DataTable dtB = dtA.Clone();
            dtB.Rows.Add(1, "100");
            dtB.Rows.Add(3, "100");

            DataTable dtC = dtA.Clone();
            dtC.Columns.Add("price_excel");

            var leftData = from a in dtA.AsEnumerable()
                           join b in dtB.AsEnumerable()
                           on a.Field<int>("id") equals b.Field<int>("id") into g
                           from b in g.DefaultIfEmpty()
                           select new
                           {
                               id = a.Field<int>("id"),
                               price = a.Field<string>("price"),
                               price_excel = b == null ? "Null" : b.Field<string>("price")
                           };
            var rightData = from b in dtB.AsEnumerable()
                            where !dtA.AsEnumerable().Select(a => a.Field<int>("id")).Contains(b.Field<int>("id"))
                            select new
                            {
                                id = b.Field<int>("id"),
                                price = "Null",
                                price_excel = b.Field<string>("price")
                            };

            var fullJoinData = leftData.Union(rightData);
            fullJoinData.ToList().ForEach(q => dtC.Rows.Add(q.id, q.price, q.price_excel));
             Console.ReadLine();

        }
        


主要是
 on a.Field<int>("id") equals b.Field<int>("id") into g  这句 ,into g ,怎么都出错啊~

------解决方案--------------------
不应该啊。。。NET3。5以上都支持的啊:

http://msdn.microsoft.com/en-us/library/bb534297(v=vs.90).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2