Lambda 联接错误

本文关键字:错误 Lambda | 更新日期: 2023-09-27 17:56:11

我在连接两个表时遇到问题,并继续收到"无法从用法推断。尝试显式指定类型参数。

"错误 2 方法的类型参数 'System.Linq.Enumerable.Join(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, System.Func, System.Func, 系统.Func)' 不能从 用法。尝试显式指定类型参数。 C:''Pro Asp.net MVC 5''第13章''1''体育商店 - Copy''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; }
}

Lambda 联接错误

docProduct => docProduct,

这一行应改为

docProduct => docProduct.Doc_Id

因为这是您要加入的关键

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

public void MyMethod<T>(T input)

你可以这样称呼它:

MyMethod<int>(0);

但实际上,这个<int>是不必要的,所以你可以写:

MyMethod(0);

由于 0 是 int,因此编译器可以计算出T必须是 int。

但是,如果您有:

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

你用这个打电话给

MyMethod(0, "Hello");

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

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