使用LINQ从不同的表中拉出不同的列 [英] Pulling out different columns from different tables using LINQ

查看:79
本文介绍了使用LINQ从不同的表中拉出不同的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表:

实体
ID(PK),整数
名称
描述

Entity
ID (PK), int
Name
Descrip

用户
ID(PK)
EntityID,int(未连接到Entity表)

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

现在我正在使用LINQ来提取具有Entity.ID =某些东西的记录.它将在GridView中向我显示几条记录.

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.

这是我的LINQ语句:

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':

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!

推荐答案

基本上,您收到的错误告诉您编译器不知道比较两个值的方法,因为一个值与另一个值不同.您确定不将stringint进行比较吗?如果是这种情况,您显然可以解析字符串,如下所示:

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

这篇关于使用LINQ从不同的表中拉出不同的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆