开发者

linq to sql using foreign keys returning iqueryable(of myEntity]

开发者 https://www.devze.com 2022-12-28 22:42 出处:网络
I\'m trying to use Linq to SQL to return an IQueryable(of Project) when using foreign key relationships. Using the below schema, I want to be able to pass in a UserId and get all the projects created

I'm trying to use Linq to SQL to return an IQueryable(of Project) when using foreign key relationships. Using the below schema, I want to be able to pass in a UserId and get all the projects created for the company the user is associated with.

DB tables:

Projects
  Projid
  ProjCreator FK (UserId from UserInfo table)
  Companyid FK (CompanyID from Companies table)

UserInfo
  UserID PK
  开发者_StackOverflowCompanyid FK

Companies
 CompanyId PK
 Description

I can get the iqueryable(of project) when simply getting the ProjectCreator with this:

Return (From p In db.Projects _
 Where p.ProjectCreator = Me.UserId)

But I'm having trouble getting the syntax to get a iqueryable(of projects) when using foreign keys. Below gives me an IQueryable(of anonymous) but I can't seem to convince it to give me an IQueryable(of project) even if I try to cast it:

  Dim retval = (From p In db.Projects _
    Join c In db.Companies On p.CompanyId Equals c.CompanyId _
     Join u In db.UserInfos On u.CompanyId Equals c.CompanyId _
      Where u.Login = UserId)


Simply select the project:

Dim retval = (From p In db.Projects _
  Join c In db.Companies On p.CompanyId Equals c.CompanyId _
   Join u In db.UserInfos On u.CompanyId Equals c.CompanyId _
    Where u.Login = UserId _
     Select p)
0

精彩评论

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