连接数据表和集合并返回LINQ C#中的列表

时间:2013-12-23 05:39:03

标签: c# linq list datatable

我有一个dataTable Dt

**Code    value   stdcode   desc****
  code1   value1  abcd12     abcjkdh
  code2   value2  cdfg34     sdfiuysd
   .       .       .          .
   .       .       .           .

我有一个列表stdallCodes

**stdcode   desc**
  abcd12    sdfsd
  fhry67    uisydif

我想在stdcode上使用linq加入这两个

请提供解决方案

我已尝试将数据表转换为列表并尝试加入

 List<stdcode> listCodes = new List<stdcode>();
     listCodes = (from row in dataTable.AsEnumerable() select row).ToList<stdcode>();


List<stdcode> ResultCodes = from codes in stdallCodes join dataCode in listCodes on codes.stdcode equals dataCode.stdcode select codes;

某些错误未转换

我想要一个包含stdcode,代码和值列

的结果列表

2 个答案:

答案 0 :(得分:2)

试试这个: -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace DataTableJoin
{
    class Program
    {
        class StdCode
        {
            public string STDCode { get; set; }
            public string Description { get; set; }
        }
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            DataColumn dc = new DataColumn();
            dt.Columns.Add(new DataColumn("Code"));
            dt.Columns.Add(new DataColumn("Value"));
            dt.Columns.Add(new DataColumn("STDCode"));
            dt.Columns.Add(new DataColumn("Description"));

            DataRow row = dt.NewRow();
            row["Code"] = "code1";
            row["Value"] = "value1";
            row["STDCode"] = "abcd12";
            row["Description"] = "abcjkdh";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["Code"] = "code2";
            row["Value"] = "value2";
            row["STDCode"] = "cdfg34";
            row["Description"] = "cdfg34";
            dt.Rows.Add(row);

            List<StdCode> listCodes = new List<StdCode>()
            {
                new StdCode(){ STDCode = "abcd12", Description="sdfsd"},
                new StdCode(){ STDCode="fhry67", Description = "uisydif"}
            };


            var query = dt.AsEnumerable().
                                    Select(item => new
                                    {
                                        Code = item.Field<string>("Code"),
                                        Value = item.Field<string>("Value"),
                                        STDCode = item.Field<string>("STDCode"),
                                        Description = item.Field<string>("Description")
                                    });

            var query1 = listCodes.Join(query, x => x.STDCode, y => y.STDCode, (x, y) => y);

            Console.WriteLine("Code\tValue\tSTDCode\tDescription");
            foreach (var r in query1)
            {
                Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}", r.Code , r.Value , r.STDCode , r.Description));
            }
        }
    }
}

<强>输出: -

Code    Value   STDCode Description
code1   value1  abcd12  abcjkdh
Press any key to continue . . .

修改

var query1 = listCodes.Join(query, x => x.STDCode, y => y.STDCode, (x, y) => new{
                y.Code,
                y.Value,
                y.STDCode,
                y.Description,
                x.MyField           
            } );

答案 1 :(得分:-1)


尝试更简单:

 class Program
        {
            static void Main(string[] args)
            {
                DataTable dtName = new DataTable();
                dtName.Columns.Add("Code");
                dtName.Columns.Add("FirstName");
                dtName.Columns.Add("lastName");

                dtName.Rows.Add("1", "Abhishek", "Shukla");
                dtName.Rows.Add("2", "Deepak", "Singh");
                dtName.Rows.Add("3", "Vinay", "Negi");

                DataTable dtHomeTowns = new DataTable();
                dtHomeTowns.Columns.Add("Code");
                dtHomeTowns.Columns.Add("HomeTown");
                dtHomeTowns.Columns.Add("State");

                dtHomeTowns.Rows.Add("1", "Ajmer", "Rajasthan");
                dtHomeTowns.Rows.Add("2", "Bhiwadi", "Rajasthan");
                dtHomeTowns.Rows.Add("3", "Guwahati", "Orissa");

                var list = (from names in dtName.AsEnumerable()
                            join town in dtHomeTowns.AsEnumerable() on names["Code"] equals town["Code"]
                            select new
                            {
                                Code = names["Code"],
                                FirstName = names["FirstName"],
                                lastName = names["lastName"],
                                HomeTown = town["HomeTown"],
                                State = town["State"],
                            }).ToList();
                foreach (var item in list)
                {
                    Console.WriteLine(String.Format("Code:{0}, FirstName: {1},LastName :                 {2},HomeTown:{3},State:{4}", item.Code, item.FirstName, item.lastName, item.HomeTown, item.State));
                }

                Console.ReadKey();
            }
        }

希望这有帮助!