无法获取c#linq查询以连接编译 [英] Can't get c# linq query to compile with joins

查看:120
本文介绍了无法获取c#linq查询以连接编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一些c#代码的例子,我不能在做一些linq连接时编译。有人知道为什么这不编译?



错误是


无法从查询中推断类型参数


(在我的实际代码 Fetch c>使用System.Collections返回 IQueryable< T>

 一般; 
using System.Linq;

namespace LinqJoin
{
public class DataRepository< T>
{
public IList< T> Fetch()
{
return new List< T>();
}
}

内部类SSOUser
{
public int Id {get;组; }
}

内部类UserRole
{
public int SSOUserId {get;组; }
public int RoleId {get;组; }
}

内部类角色
{
public int RoleId {get;组; }
}

class Program
{
static void Main(string [] args)
{
var users = new DataRepository< SSOUser> ;()。取();
var userroles = new DataRepository< UserRole>()。Fetch();
var roles = new DataRepository< Role>()。Fetch();

var result = from u in users
在userroles中的u上的join ur.Id等于ur.SSOUserId
在r.RoleId上的角色中的join r等于ur.RoleId
选择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);
}
}
}


解决方案>

此连接是错误的方式:

  r上的角色中的join r等于ur.RoleId 

应该是:

 在角色上加入r在ur.RoleId等于r.RoleId 

引入总是必须在 equals right 手边。通常,编译器很好地告诉你,在错误消息,记住你...


Below is a cut down example of some c# code I can't get to compile while doing some linq joins. Does anyone know why this doesn't compile?

The error is

Type arguments cannot be inferred from the query

(In my real code Fetch() returns an 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);
        }
    }
}

解决方案

This join is the wrong way round:

join r in roles on r.RoleId equals ur.RoleId

It should be:

join r in roles on ur.RoleId equals r.RoleId

The range variable you introduce always has to be on the right hand side of the equals. Normally the compiler is pretty good about telling you that in the error message, mind you...

这篇关于无法获取c#linq查询以连接编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆