无法获取c#linq查询以连接编译

编程入门 行业动态 更新时间:2024-10-28 10:32:06
本文介绍了无法获取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查询以连接编译

本文发布于:2023-07-23 06:37:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1194821.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!