简体   繁体   English

LINQ Join因类型不匹配而失败

[英]LINQ Join fails because of type mismatch

I need data from 2 different databases. 我需要来自2个不同数据库的数据。 I have tried the following 我尝试了以下

var User = (from U in _db.TblUsers
                        where U.IsAdmin == false
                        select U).ToList();

var AspNewsUser = (from A in _dbAspNet.aspnet_Users                        
                   select A).ToList();


var result = (from U in User
              join A in AspNewsUser
              on U.UserID equals A.UserId
              select U);

But I get the following error message 但是我收到以下错误消息

The type of one of the expressions in the join clause is incorrect. join子句中的表达式之一的类型不正确。 Type inference failed in the call to 'Join'. 调用“加入”时类型推断失败。

As you can see in the table schema of aspnet_Users , Userid is a uniqueidentifier (maps to GUID in entity framework). 如您在aspnet_Users表模式中所看到的, Userid是一个唯一uniqueidentifier (映射到实体框架中的GUID)。 I bet that TblUsers.UserID has a different type (like int ). 我打赌TblUsers.UserID具有不同的类型(如int )。

Use projection, make them the same anonymous type. 使用投影,使它们成为相同的匿名类型。

    var Users = (from U in _db.TblUsers
                    where U.IsAdmin == false
                    select new { UserID = U.UserID }).ToList();

    var AspNewsUsers = (from A in _dbAspNet.aspnet_Users                        
                    select new { UserID = A.UserID }).ToList();


    var result = (from i in Users join o in AspNewUsers on i.UserID equals o.UserID).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM