简体   繁体   English

如何根据涉及继承的模型更改来修改此联接?

[英]How do I modify this join in accordance with a model change involving inheritance?

I made a change to the model of my project, and there is a join that I can't seem to figure out how to change in response to the model change. 我对项目的模型进行了更改,但有一个联接似乎无法弄清如何响应模型更改而进行更改。 Here is the original model setup, and the join code: 这是原始的模型设置以及连接代码:

旧关系

           query = query.Join(db.Reports,
                x => new
                {
                    x.SourceUser,
                    x.TargetUser
                },
                x2 => new
                {
                    SourceUser = x2.TargetUser,
                    TargetUser = x2.SourceUser
                },
                (x, x2) => new { x, x2 }).Where(f => (f.x.SourceUser == user)).Select(p => p.x);

The result of this join is that the resulting query provides only the report relationships that are mutual (as in, both parties have reported eachother). 这种联接的结果是,结果查询仅提供相互之间的报告关系(例如,双方都相互报告了)。 This join works perfectly fine, until I make the model change. 直到我进行模型更改之前,此连接都可以正常工作。

Here is the new model setup 这是新的模型设置

新关系[

and here is my attempt at modifying the join code 这是我尝试修改联接代码的尝试

              query = query.Join(db.Reports,
                x => new
                {
                    x.SourceUser,
                    x.TargetReportable
                },
                x2 => new
                {
                    SourceUser = x2.TargetReportable,
                    TargetReportable = x2.SourceUser
                },
                (x, x2) => new { x, x2 }).Where(f => (f.x.SourceUser == user)).Select(p => p.x);

This results in the following error: 这将导致以下错误:

The type arguments for method 'System.Linq.Enumerable.Join(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, System.Func, System.Func, System.Func) cannot be inferred from the usage. 无法从用法中推断方法'System.Linq.Enumerable.Join(System.Collections.Generic.IEnumerable,System.Collections.Generic.IEnumerable,System.Func,System.Func,System.Func)的类型参数。 Try specifying the type arguments explicitly. 尝试显式指定类型参数。

I'm assuming that SourceUser is of type UserAccount and TargetReportable is of type Reportable. 我假设SourceUser的类型为UserAccount,TargetReportable的类型为Reportable。 The lambda is comparing apples to oranges,. λ比较苹果和桔子。 you need to cast sourceuser to type Reportable since it inherits Reportable. 您需要强制sourceuser键入Reportable,因为它继承了Reportable。

 query = query.Join(db.Reports,
                x => new
                {
                    T = (Reportable)x.SourceUser,
                    x.TargetReportable
                },
                x2 => new
                {
                    SourceUser = x2.TargetReportable,
                    TargetReportable = (Reportable)x2.SourceUser
                },
                (x, x2) => new { x, x2 }).Where(f => (f.x.SourceUser == user)).Select(p => p.x);

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

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