C# 数据表、超越、联接

C# 数据表、超越、联接,c#,visual-studio-2010,linq,C#,Visual Studio 2010,Linq,我有一个名为sourceTable的数据表,其中包含source\u Id、title和programme\u Id列。第二个数据表是credits,列为credit\u Id,programme\u Id。所有列都是Int类型,而不是列标题 datatablecredits中的列program\u Id是datatablesourceTable 我想要实现的是从datatablecredits中使用列credit\u Id超越tablesourceTable 我写了一个代码,但速度很慢,有更好

我有一个名为
sourceTable
的数据表,其中包含
source\u Id
title
programme\u Id
列。第二个数据表是
credits
,列为
credit\u Id
programme\u Id
。所有列都是Int类型,而不是列标题

datatable
credits
中的列
program\u Id
是datatable
sourceTable

我想要实现的是从datatable
credits
中使用列
credit\u Id
超越table
sourceTable

我写了一个代码,但速度很慢,有更好的方法吗!,FirstOrDefault将在没有我正在查找的项的情况下放置0,对于这种情况,最好返回null值而不是0

sourceTable.columns.Add("credits_Id");
var rowColl = credits.AsEnumerable();
foreach (DataRow row in sourceTable.Rows)
{
    var credits_Id =
        (from r in rowColl
         where r.Field<int>("programme_Id") == Convert.ToInt32(row["programme_Id"].ToString())
         select r.Field<int>("credits_Id")).FirstOrDefault<int>();

    row["credits_Id"] = credits_Id;
}
sourceTable.columns.Add(“credits_Id”);
var rowColl=credits.AsEnumerable();
foreach(sourceTable.Rows中的DataRow行)
{
var信用证=
(来自rowColl的r)
其中r.Field(“program_Id”)==Convert.ToInt32(行[“program_Id”].ToString())
选择r.Field(“credits_Id”).FirstOrDefault();
行[“信用卡Id”]=信用卡Id;
}

它的工作速度很慢,因为您要针对源表中的每一行迭代credits表中的所有行。您可以使用以下linq查询联接这两个表

(from sourceRow in sourceTable.Rows.OfType<DataRow>()
join creditRow in credits.Rows.OfType<DataRow>()
   on sourceRow.Field<int>("programme_Id") equals creditRow.Field<int>("programme_Id")
select new {sourceRow, creditRow})
   .ForEach(o => o.sourceRow["credits_id"] = o.creditRow["sourceRow"]);
(来自sourceTable.Rows.OfType()中的sourceRow)
在credits.Rows.OfType()中加入creditRow
在sourceRow.Field上(“程序Id”)等于creditRow.Field(“程序Id”)
选择“新建”{sourceRow,creditRow})
.ForEach(o=>o.sourceRow[“credits_id”]=o.creditRow[“sourceRow”]);

它的工作速度很慢,因为您要针对源表中的每一行迭代credits表中的所有行。您可以使用以下linq查询联接这两个表

(from sourceRow in sourceTable.Rows.OfType<DataRow>()
join creditRow in credits.Rows.OfType<DataRow>()
   on sourceRow.Field<int>("programme_Id") equals creditRow.Field<int>("programme_Id")
select new {sourceRow, creditRow})
   .ForEach(o => o.sourceRow["credits_id"] = o.creditRow["sourceRow"]);
(来自sourceTable.Rows.OfType()中的sourceRow)
在credits.Rows.OfType()中加入creditRow
在sourceRow.Field上(“程序Id”)等于creditRow.Field(“程序Id”)
选择“新建”{sourceRow,creditRow})
.ForEach(o=>o.sourceRow[“credits_id”]=o.creditRow[“sourceRow”]);

以防谷歌把某人带到这里:这是我现在提出的问题的解决方案:)

var q=来自sourceTable.AsEnumerable()中的c
在积分中加入o。c.Field(“program_Id”)上的AsEnumerable()等于o.Field(“program_Id”)进入外部
从outer.DefaultIfEmpty()中的o开始
选择新的
{
标题=c.字段(“标题”),
credits\u Id=(o==null)?-1:o.Field(“credits\u Id”)
};
var qToList=q.ToList();
现在我们可以将此列表转换为Datatable:

public static DataTable ListToDataTable<T>(List<T> list)
    {
        DataTable dtToConvert = new DataTable();
        try
        {

        foreach (PropertyInfo info in typeof(T).GetProperties())
        {
            dtToConvert.Columns.Add(new DataColumn(info.Name, info.PropertyType));
        }
        foreach (T t in list)
        {
            DataRow row = dtToConvert.NewRow();
            foreach (PropertyInfo info in typeof(T).GetProperties())
            {
                row[info.Name] = info.GetValue(t, null);
            }
            dtToConvert.Rows.Add(row);
        }

        } catch(Exception ex)
            {


            }
        return dtToConvert;
    }
公共静态数据表ListToDataTable(列表)
{
DataTable dtToConvert=新DataTable();
尝试
{
foreach(typeof(T).GetProperties()中的PropertyInfo信息)
{
添加(新数据列(info.Name,info.PropertyType));
}
foreach(列表中的T)
{
DataRow row=dtToConvert.NewRow();
foreach(typeof(T).GetProperties()中的PropertyInfo信息)
{
行[info.Name]=info.GetValue(t,空);
}
dtToConvert.Rows.Add(row);
}
}捕获(例外情况除外)
{
}
返回数据转换;
}

干杯

以防谷歌把某人带到这里:这是我现在提出的问题的解决方案:)

var q=来自sourceTable.AsEnumerable()中的c
在积分中加入o。c.Field(“program_Id”)上的AsEnumerable()等于o.Field(“program_Id”)进入外部
从outer.DefaultIfEmpty()中的o开始
选择新的
{
标题=c.字段(“标题”),
credits\u Id=(o==null)?-1:o.Field(“credits\u Id”)
};
var qToList=q.ToList();
现在我们可以将此列表转换为Datatable:

public static DataTable ListToDataTable<T>(List<T> list)
    {
        DataTable dtToConvert = new DataTable();
        try
        {

        foreach (PropertyInfo info in typeof(T).GetProperties())
        {
            dtToConvert.Columns.Add(new DataColumn(info.Name, info.PropertyType));
        }
        foreach (T t in list)
        {
            DataRow row = dtToConvert.NewRow();
            foreach (PropertyInfo info in typeof(T).GetProperties())
            {
                row[info.Name] = info.GetValue(t, null);
            }
            dtToConvert.Rows.Add(row);
        }

        } catch(Exception ex)
            {


            }
        return dtToConvert;
    }
公共静态数据表ListToDataTable(列表)
{
DataTable dtToConvert=新DataTable();
尝试
{
foreach(typeof(T).GetProperties()中的PropertyInfo信息)
{
添加(新数据列(info.Name,info.PropertyType));
}
foreach(列表中的T)
{
DataRow row=dtToConvert.NewRow();
foreach(typeof(T).GetProperties()中的PropertyInfo信息)
{
行[info.Name]=info.GetValue(t,空);
}
dtToConvert.Rows.Add(row);
}
}捕获(例外情况除外)
{
}
返回数据转换;
}

干杯

这两个数据表都有一些由xml文档填充的内容。我认为您不需要在convert.ToInt32(row[“program_Id”]”中转换为ToString。ToString()有一点帮助(一分钟:),我有点麻烦,因为有很多行。两个数据表都有一些xml文档填充的内容。我认为您不需要在convert.ToInt32(row[“program_Id”]).ToString()中转换为ToString(row[“program_Id”])。这有点帮助(一分钟:),我有点麻烦,因为有很多行