Pulling out different columns from different table

2019-09-04 17:02发布

问题:

I have two tables:

Entity
ID (PK), int
Name
Descrip

Users
ID (PK)
EntityID, int (this is not connected to Entity table)

Now I am using LINQ to pull the records which has a Entity.ID = something. Which will show me couple of records in my GridView.

Here is my LINQ statement:

protected void Page_Load(object sender, EventArgs e)
{
    string getEntity = Request.QueryString["EntityID"];
    int getIntEntity = Int32.Parse(getEntity);
    OISEntityTestingDataContext db = new OISEntityTestingDataContext();
    //OISLinqtoSQLDataContext db = new OISLinqtoSQLDataContext();
    var tr =
        from r in db.Users
        join s in db.Entities on r.UserID equals s.ID
        where s.ID == getIntEntity
        select new
        {
            //To Show Items in GridView!
        };

    GridView1.DataSource = tr;
    GridView1.DataBind();
}

Now here I am getting an error mesg on 'join':

The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.

What does that mean? Can someone please help me on this. Thank you!

回答1:

Basically the error you receive tells you that the compiler does not know a way to compare the two values because one is different from the another. Are you sure that you are not comparing a string to an int? If this is your case you can obviously parse the string as shown below:

var tr =
    from r in db.Users
    join s in db.Entities on int.Parse(r.UserID) equals s.ID
    where s.ID == getIntEntity
    select new
    {
        //To Show Items in GridView!
    };


回答2:

I'd wager that the type of one of your join conditions doesn't match the type of the other.

The type of one of the expressions in the join clause is incorrect.

Make sure they match in your entity mappings.

In your post you list your tables as having a column "ID" but in the join one is ID and the other is UserID. Are you sure your joining on the correct column?