Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Linq连接合并数据表时,错误无法隐式转换类型_C#_Asp.net_Linq - Fatal编程技术网

C# 使用Linq连接合并数据表时,错误无法隐式转换类型

C# 使用Linq连接合并数据表时,错误无法隐式转换类型,c#,asp.net,linq,C#,Asp.net,Linq,我必须显示来自xml文件的聊天。但是当我连接两个合并的表时,会出现一些错误 错误是: 无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换。是否缺少强制转换?D:\ChatReport\ChatReport\Default.aspx.cs 109 42 ChatReport 您专门为DataRow类型的IEnumerable调用,然后在其中选择一个IEnu

我必须显示来自xml文件的聊天。但是当我连接两个合并的表时,会出现一些错误

错误是:


无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换。是否缺少强制转换?D:\ChatReport\ChatReport\Default.aspx.cs 109 42 ChatReport


您专门为DataRow类型的IEnumerable调用,然后在其中选择一个IEnumerable匿名类型。您需要指定匿名类型实际上是DataRow对象:

IEnumerable<DataRow> query = from dtquer in dtmsgbody.AsEnumerable()
                                 join dtusr in dtbodyuser.AsEnumerable()
                                 on dtquer.Field<string>("userId") equals
                                 dtusr.Field<string>("userId")

                                 select new DataRow
                                 [

                                     dtquer.Field<string>("userId"),
                                     dtusr.Field<string>("userNick"),
                                     dtquer.Field<int>("timeShift"),
                                     dtquer.Field<int>("message_Id"),
                                     dtquer.Field<string>("msgText_Text")

                                 ];
或者,使用var降低查询对象的特定性,以便您选择的类型可以指定IEnumerable的组成:

var query = from dtquer in dtmsgbody.AsEnumerable()
                                 join dtusr in dtbodyuser.AsEnumerable()
                                 on dtquer.Field<string>("userId") equals
                                 dtusr.Field<string>("userId")

                                 select new  
                {

                    userId = dtquer.Field<string>("userId"),
                    userNick = dtusr.Field<string>("userNick"),
                    timeShift = dtquer.Field<int>("timeShift"),
                    message_Id = dtquer.Field<int>("message_Id"),
                    msgText_Text = dtquer.Field<string>("msgText_Text")


                };
虽然稍后在CopyToDataTable的代码中需要DataRows,但就我的钱而言,我自己会采用第一种方法,而不是将var用于需要转换的匿名类型

编辑

回应评论中的呼吁,摆脱对复制的依赖可能是更好的解决方案。通过定义表结构,然后将所需的值分配给相应的列,可以解决转换问题

DataTable dt10 = new DataTable();

dt10.Columns.Add("UserId");
dt10.Columns.Add("Nickname");
dt10.Columns.Add("TimeShift");
dt10.Columns.Add("MessageId");
dt10.Columns.Add("MessageText");

var query = from dtquer in dtmsgbody.AsEnumerable()
            join dtusr in dtbodyuser.AsEnumerable()
            on dtquer.Field<string>("userId") equals
            dtusr.Field<string>("userId")

            select new
            {

                userId = dtquer.Field<string>("userId"),
                userNick = dtusr.Field<string>("userNick"),
                timeShift = dtquer.Field<int>("timeShift"),
                message_Id = dtquer.Field<int>("message_Id"),
                msgText_Text = dtquer.Field<string>("msgText_Text")


             };

var counter = 0;

foreach (var row in query)
{
     dt10.NewRow();
     var newRow = dt10.Rows[counter];

     newRow.ItemArray[0] = row.userId;
     newRow.ItemArray[1] = row.userNick;
     newRow.ItemArray[2] = row.timeShift;
     newRow.ItemArray[3] = row.message_Id;
     newRow.ItemArray[4] = row.msgText_Text;

     counter++;
 }

 // Should end up with dt10 having all the results from the query above loaded into it

在没有依赖项的情况下很难进行检查,但您报告的初始错误不再显示在my Visual Studio中。

请注意,您希望得到IEnumerable类型的查询结果,但该查询返回您在select new中生成的匿名类型的对象列表。将IEnumerable查询更改为var查询并使用匿名类型结果集,或者将查询更改为返回DataRow实例的集合。如果更改为var查询,则不会出现错误,但重要的是函数DT10=query.CopyToDataTable;它不起作用。如何使用anonymos查询?您能更具体一点吗?什么不起作用,以什么方式?这些是我的合并表。dtmsg=ds.tables[message];dtmsgtxt=ds.Tables[msgText];dtuserinfo=ds.Tables[userInfo];dtnewparty=ds.Tables[newParty];dtbodyuser=dtnewparty.Copy;dtbodyuser.Mergedtuserinfo;dtbodyuser.AcceptChanges;数据表dt=dtbodyuser;dtmsgbody=dtmsg.Copy;dtmsgbody.Mergedtmsgtxt;dtmsgbody.AcceptChanges;我需要将所有文件合并到一个表中。当我尝试时,您的查询不起作用。我不熟悉linq查询。请帮助无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换。是否缺少强制转换?D:\ChatReport\ChatReport\Default.aspx.cs 109 42 ChatReport您可以查看我以前的代码吗?我需要最终解决方案来获得一个包含用户ID、昵称、timeshift、MessageText的表。我知道这是编译器错误,不是异常我的错误。我还更新了我的答案,有点笨重,但应该可以让你绕过这个街区。抱歉,回复太晚,请选择“新建”{userId=dtquery.FielduserId,userNick=dtusr.FielduserNick,timeShift=dtquery.FieldtimeShift,message_Id=dtquery.Fieldmessage_Id,msgText_Text=dtquery.FieldmsgText_Text};这段代码对我有用,我在Slide selectcommand中转换为字符串和时间日期evry字段。非常感谢
DataTable dt10 = new DataTable();

dt10.Columns.Add("UserId");
dt10.Columns.Add("Nickname");
dt10.Columns.Add("TimeShift");
dt10.Columns.Add("MessageId");
dt10.Columns.Add("MessageText");

var query = from dtquer in dtmsgbody.AsEnumerable()
            join dtusr in dtbodyuser.AsEnumerable()
            on dtquer.Field<string>("userId") equals
            dtusr.Field<string>("userId")

            select new
            {

                userId = dtquer.Field<string>("userId"),
                userNick = dtusr.Field<string>("userNick"),
                timeShift = dtquer.Field<int>("timeShift"),
                message_Id = dtquer.Field<int>("message_Id"),
                msgText_Text = dtquer.Field<string>("msgText_Text")


             };

var counter = 0;

foreach (var row in query)
{
     dt10.NewRow();
     var newRow = dt10.Rows[counter];

     newRow.ItemArray[0] = row.userId;
     newRow.ItemArray[1] = row.userNick;
     newRow.ItemArray[2] = row.timeShift;
     newRow.ItemArray[3] = row.message_Id;
     newRow.ItemArray[4] = row.msgText_Text;

     counter++;
 }

 // Should end up with dt10 having all the results from the query above loaded into it