使用多对多场景和其他表编写linq查询

时间:2014-01-03 21:33:03

标签: c# sql linq-to-sql fluent ef-migrations

我想写一个linq查询,它连接一些表并返回一个自定义对象但是我遇到了很多表的问题,因为我没有任何对象可以使用。你最后会看到我有问题的linq查询但是现在让我告诉你我对Code First,流畅的API和SQL的看法:

以下是我需要使用的所有表(稍后您将看到带有sql语句的所有外键):

  1. 用户配置

  2. BusinessProfile

  3. 出价

  4. 招标

  5. UserBusinessProfile 用户和商家资料的多对多表格

  6. 多对多的表格以protected override void OnModelCreating(DbModelBuilder modelBuilder)这种方式定义

    // Many to many UserProfiles => BusinessProfiles
                modelBuilder.Entity<UserProfile>()
                    .HasKey(primaryKey => primaryKey.Id)
                    .HasMany(business => business.BusinessProfiles)
                    .WithMany(user => user.UserProfiles)
                    .Map(m => m.MapLeftKey("UserProfileId")
                    .MapRightKey("BusinessProfileId")
                    .ToTable("UserBusinessProfile"));
    

    就在这里,我需要在我的linq查询中包含.ToTable(“UserBusinessProfile”)......

    最后,我的 SQL语句我需要用linq编写(SQL语句按预期工作。):

    select * from UserProfile inner join UserBusinessProfile on UserProfile.Id = UserBusinessProfile.UserProfileId inner join BusinessProfile on BusinessProfile.Id = UserBusinessProfile.BusinessProfileId inner join Bid on Bid.UserProfileId = UserProfile.Id and Bid.BusinessProfileId = BusinessProfile.Id inner join Tender on Bid.TenderId = Tender.Id
    

    我有linq查询:

    from UserProfile in context.UserProfile
    join UserBusinessProfile in context.UserBusinessProfile on new { Id = UserProfile.Id } equals new { Id = UserBusinessProfile.UserProfileId }
    join BusinessProfile in context.BusinessProfile on new { Id = UserBusinessProfile.BusinessProfileId } equals new { Id = BusinessProfile.Id }
    join Bid in context.Bid
          on new { UserProfileId = UserProfile.Id, BusinessProfileId = BusinessProfile.Id }
      equals new { Bid.UserProfileId, Bid.BusinessProfileId }
    join Tender in context.Tender on new { TenderId = Bid.TenderId } equals new { TenderId = Tender.Id }
    where
      UserProfile.Id == 1 &&
      BusinessProfile.Id == 1
    select new CustomObject{
      ...
    }
    

    正如您所看到的,我无法使用linq查询中的context.UserBusinessProfile,因为我没有任何对象可以像其他表一样使用。我真的不知道如何做到这一点,或者我如何编写我的linq查询来完成任务。

    感谢您的时间和帮助,

    卡琳


    换句话说,当您知道UserBusinessProfile是多对多的表时,如何从此SQL查询中编写linq查询?:

    select * from UserProfile 
    inner join UserBusinessProfile on UserProfile.Id = UserBusinessProfile.UserProfileId
    inner join BusinessProfile on BusinessProfile.Id = UserBusinessProfile.BusinessProfileId
    inner join Bid on Bid.UserProfileId = UserProfile.Id and Bid.BusinessProfileId = BusinessProfile.Id
    inner join Tender on Bid.TenderId = Tender.Id
    where UserProfile.Id = 1 and BusinessProfile.Id = 1
    

2 个答案:

答案 0 :(得分:0)

在LINQ中编写应该要容易得多。

from ubp in context.UserBusinessProfile 
where ubp.UserProfile.Id = 1 && ubp.BusinessProfile.Id = 1
select ubp.UserProfile
//you can pretty much do anything with Bid and Tender, 
//you should be able to access Bid from
//ubp.BusinessProfile.Bids 
//(I think multiple bids are available for a BusinessProfile)

答案 1 :(得分:0)

我找到了要走的路。可能是它可以优化,但它的工作。

    from user in context.UserProfiles
                    from business in context.BusinessProfiles
                    join bid in context.Bids on new { UserProfileId = user.Id, BusinessProfileId = business.Id } equals new { bid.UserProfileId, bid.BusinessProfileId }
                    join tender in context.Tenders on new { TenderId = bid.TenderId } equals new { TenderId = tender.Id }
                    where business.Id == businessProfileId && user.Id == userProfileId && tender.Id == tenderId
select new CustomObject
{
...
}