开发者

Pulling out different columns from different tables using LINQ

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();
    //O开发者_如何转开发ISLinqtoSQLDataContext 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!


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜