1
votes

I had troubles joining two Tables and continued to receive the "cannot be inferred from the usage. Try specifying the type arguments explicitly.".

"Error 2 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. Try specifying the type arguments explicitly. C:\Pro Asp.net Mvc 5\Chapter 13\1\SportsStore - Copy\SportsStore.WebUI\Controllers\DocProductController.cs 29 17 SportsStore.WebUI"

Can anybody help me?

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; }
}
1

1 Answers

0
votes
docProduct => docProduct,

This line should instead read

docProduct => docProduct.Doc_Id

Since that's the key you're joining on

Generally, what that message refers to is the fact that when you call a generic method, it tries to infer the type arguments. e.g.

public void MyMethod<T>(T input)

You could call this like:

MyMethod<int>(0);

But actually, that <int> is unnecessary, so you can just write:

MyMethod(0);

And because 0 is an int, the compiler can work out that T must be int.

But what if you had:

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

And you called this with

MyMethod(0, "Hello");

Now you'd see an error messages similar to the one you received, because there's no sensible type T for it to infer- either for a string, or an int, there'd be one argument wrong.

So generally, that message indicates one of your parameters is of the wrong type. Occasionally it can also appear in situations where the type is right, but there's some ambiguity which causes it to be impossible for the compiler to work out the generic types. If you're not sure how the parameters are of the wrong type, you can try specifying them explicitly, like in the first example of how you'd call MyMethod. At the very least, it might give you a more informative error about where the type mismatch occurs.