使用LINQ连接两个表

时间:2011-04-06 19:25:59

标签: c# asp.net linq linq-to-sql

我有两张桌子:

  

PlanMaster(PlanName,Product_ID)

  

ProductPoints(Entity_ID,Product_ID,Comm1,Comm2)

现在我将Entity_ID存储到会话中,该会话存储在'int'中:

int getEntity = Int16.Parse(Session["EntitySelected"].ToString());

我想在我的LINQ查询中显示上表中具有

的所有项目
  

Entity_ID = getEntity

这是我的LINQ查询:

var td = from s in cv.Entity_Product_Points join r in dt.PlanMasters on s.Product_ID equals r.Product_ID
         where s.Entity_ID = getEntity
         select s;

现在它给我一个错误,上面写着:

  

无法隐式转换类型'int?' 'bool'

这里出了什么问题?感谢您提前的意见!

6 个答案:

答案 0 :(得分:13)

尝试将其更改为

 where s.Entity_ID == getEntity

答案 1 :(得分:7)

var td =
    from s in cv.Entity_Product_Points
    join r in dt.PlanMasters on s.Product_ID equals r.Product_ID
    where s.Entity_ID == getEntity
    select s;

=不等于==

答案 2 :(得分:5)

where s.Entity_ID = getEntity应为where s.Entity_ID == getEntity

答案 3 :(得分:4)

不应该是双等于吗?

答案 4 :(得分:2)

var db1 = (from a in AccYearEntity.OBLHManifests select a).ToList();
var db2 = (from a in MasterEntity.UserMasters select a).ToList();

var query = (from a in db1
             join b in db2 on a.EnteredBy equals b.UserId
             where a.LHManifestNum == LHManifestNum
             select new { LHManifestId = a.LHManifestId, LHManifestNum = a.LHManifestNum, LHManifestDate = a.LHManifestDate, StnCode = a.StnCode, Operatr = b.UserName }).FirstOrDefault();

答案 5 :(得分:-2)

我认为这样做,

其中s.Entity_ID == getEntity