I want to write a linq query that join some tables and return a custom object but I have some problems with the many to many table because I do not have any object to work with. You will see at the end my problematic linq query but for now let's me show you what I have with Code First, fluent API and SQL:
Here are all tables I need to work with (you will see all foreign keys with the sql statement later):
UserProfile
BusinessProfile
Bid
Tender
UserBusinessProfile Many to many table for user and business profile
The many to many table is define within protected override void OnModelCreating(DbModelBuilder modelBuilder)
this way
// 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"));
Right here, the .ToTable("UserBusinessProfile") I need to include in my linq query...
Finally, my SQL Statement I need to write with linq (SQL Statement work as expected.):
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
And the linq query I have:
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{
...
}
As you can see, I cannot use context.UserBusinessProfile from my linq query because I do not have any object to work with like the other table. And I really don't know how I can do that or how I can write my linq query to make things done.
Thank you for your time and your help,
Karine
In other words, how can I write the linq query from this SQL query when you know that UserBusinessProfile is the many to many table??:
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
I found the way to go. May be it could be optimize but its working.
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
{
...
}