【问题标题】:Lambda Join ErrorLambda 连接错误
【发布时间】:2014-05-13 09:51:47
【问题描述】:

我在加入两个表时遇到了麻烦,并继续收到“无法从用法中推断出来。尝试明确指定类型参数。”。

"错误 2 方法的类型参数 'System.Linq.Enumerable.Join(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, 系统函数,系统函数, System.Func)' 不能从 用法。尝试明确指定类型参数。 C:\Pro Asp.net Mvc 5\Chapter 13\1\SportsStore - 复制\SportsStore.WebUI\Controllers\DocProductController.cs 29 17 SportsStore.WebUI"

谁能帮帮我?

public class DocProductController : Controller
{
    private IDocProductRepository repository;
    private IDocMainRepository repositoryMain;

    public DocProductController(IDocProductRepository docProductRepository, IDocMainRepository docMainRepository)
    {
        this.repository = docProductRepository;
        this.repositoryMain = docMainRepository;
    }


    public ViewResult List()
    {
        DocProductListView model = new DocProductListView
        {
            DocProduct = repository.DocProduct
            .Join(repositoryMain.DocMain,
            docProduct => docProduct,
            docMain => docMain.Doc_Id,
            (docProduct, docMain) => new { a = docMain.Doc_Id, b = docProduct.Doc_Id })
           //.OrderByDescending(n => n.DocMain)
        };

        return View(model);
    }
}



public partial class DocMain
{
    public int Doc_Id { get; set; }
    public Nullable<int> Category_Id { get; set; }
    public string Doc_Title { get; set; }
    public Nullable<int> Doc_Order { get; set; }
    public Nullable<byte> Doc_IsAudit { get; set; }
    public Nullable<int> Doc_Clicks { get; set; }
    public string Doc_TitleColor { get; set; }
    public string Doc_Author { get; set; }
    public Nullable<int> User_Id { get; set; }
    public string Doc_Source { get; set; }
    public string Doc_Thumb { get; set; }
    public Nullable<System.DateTime> Doc_DisplayTime { get; set; }
    public Nullable<System.DateTime> Doc_ReleaseTime { get; set; }
    public Nullable<System.DateTime> Doc_UpdateTime { get; set; }
    public string Doc_SEO_Title { get; set; }
    public string Doc_SEO_Description { get; set; }
    public string Doc_SEO_Keywords { get; set; }
    public string Doc_RedirectUrl { get; set; }
    public Nullable<byte> Doc_GenerateHTML { get; set; }
    public string Doc_HTMLCatagory { get; set; }
}



public partial class DocProduct
{
    public int Doc_Id { get; set; }
    public Nullable<int> Category_Id { get; set; }
    public string DocProduct_Content { get; set; }
}

【问题讨论】:

    标签: c# .net linq lambda


    【解决方案1】:
    docProduct => docProduct,
    

    这一行应该改为

    docProduct => docProduct.Doc_Id
    

    因为这是你加入的关键

    通常,该消息所指的事实是,当您调用泛型方法时,它会尝试推断类型参数。例如

    public void MyMethod<T>(T input)
    

    你可以这样称呼:

    MyMethod<int>(0);
    

    但实际上,&lt;int&gt; 是不必要的,所以你可以写:

    MyMethod(0);
    

    因为 0 是一个 int,编译器可以计算出 T 必须是 int。

    但如果你有:

    public void MyMethod<T>(T input1, T input2)
    

    你用这个来称呼它

    MyMethod(0, "Hello");
    

    现在您会看到与您收到的错误消息类似的错误消息,因为没有合理的类型 T 可以推断 - 无论是字符串还是 int,都会有一个参数错误。

    一般来说,该消息表明您的参数之一是错误的类型。有时它也可能出现在类型正确的情况下,但存在一些歧义,导致编译器无法计算出泛型类型。如果您不确定参数的类型是错误的,您可以尝试明确指定它们,就像在第一个示例中如何调用 MyMethod 一样。至少,它可能会为您提供有关类型不匹配发生位置的更多信息。

    【讨论】:

    • @user3339775 你能解释一下你为什么要加入吗?
    • @user3339775 对,但您选择的是 DocProduct。因此,您要包含的任何属性都必须是 DocProduct 上的属性。我不确定您是否更熟悉 SQL,但这就像您尝试将连接的结果插入到表中一样。您插入的联接中的字段必须是目标表上的字段。同样,您要在此处选择的属性必须是目标对象上的属性。
    • @user3339775 老实说,我会为此创建一个新问题,因为它确实是一个单独的问题,最好有问题和答案,而不仅仅是 cmets。
    • @user3339775 如果您确实创建了一个新问题,您可能应该明确说明您到底做了什么,不理解什么,以及您的知识/经验水平,因为我觉得很难判断。
    猜你喜欢
    • 2011-09-09
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2020-02-13
    相关资源
    最近更新 更多