由于类型不匹配,LINQ加入失败

时间:2012-11-25 06:49:29

标签: c# linq linq-to-sql linq-to-objects

我需要来自2个不同数据库的数据。我试过以下

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);

但是我收到以下错误消息

  

join子句中某个表达式的类型不正确。   调用“加入”时类型推断失败。

2 个答案:

答案 0 :(得分:1)

正如您在table schema of aspnet_Users中看到的那样,Useriduniqueidentifier(映射到实体框架中的GUID)。我敢打赌TblUsers.UserID有不同的类型(如int)。

答案 1 :(得分:0)

使用投影,使它们成为相同的匿名类型。

    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();