以下是一段削减版的C#代码示例,我在做一些LINQ联接时无法编译通过。有人知道为什么会编译失败吗?
错误信息为:
无法从查询中推断出类型参数
(在我的实际代码中,Fetch()
返回一个 IQueryable<T>
)
using System.Collections.Generic;
using System.Linq;
namespace LinqJoin
{
public class DataRepository<T>
{
public IList<T> Fetch()
{
return new List<T>();
}
}
internal class SSOUser
{
public int Id { get; set; }
}
internal class UserRole
{
public int SSOUserId { get; set; }
public int RoleId { get; set; }
}
internal class Role
{
public int RoleId { get; set; }
}
class Program
{
static void Main(string[] args)
{
var users = new DataRepository<SSOUser>().Fetch();
var userroles = new DataRepository<UserRole>().Fetch();
var roles = new DataRepository<Role>().Fetch();
var result = from u in users
join ur in userroles on u.Id equals ur.SSOUserId
join r in roles on r.RoleId equals ur.RoleId
select u;
//var x1 = users.Join(userroles, u => u.Id, ur => ur.SSOUserId, (u, ur) => new { User = u, UserRole = ur}).Join(roles, x => x.UserRole.RoleId, r => r.RoleId, res => res.User);
}
}
}