0

I am having 3 tables with a join tables between the user and the type tables.

enter image description here

  1. I import the tables as code first from database, I have the contexts fine, DBset, DBSet, and

        modelBuilder.Entity<User>()
            .HasMany(e => e.Type)
            .WithMany(e => e.Users)
            .Map(m => m.ToTable("UserType").MapLeftKey("ut_use_id").MapRightKey("ut_ut_id"));
    
  2. However, I have no idea how to retrieve with linq to sql a table with 3 columns User.name, User.firstname, and Type.type_description==1 ?

I started and tried

    using (MyContext db = new MyContext())
    {
        var query = (from u in db.Users....

But there is no table db.UserType ?

Thanks for your helps Cheers

dtjmsy
  • 2,664
  • 9
  • 42
  • 62
  • Perhaps some data would help but the way I see it, there is a one to many relationship between `User` and `UserType` and a one to one relationship between `UserType` and `Type`. I dont see a many-to-many relationship. – G B May 16 '15 at 21:14
  • Hi GB, the UserType actually reference the relationship between the User and UserTable, for example the id 1 of the User table to the 2 in the Type table, my tables are very much the same as http://stackoverflow.com/questions/17774373/sql-join-many-to-many – dtjmsy May 16 '15 at 21:27

1 Answers1

1

This should get you your Users (assuming the navigational properties are set up correctly, and the navigation property is UserTypes):

using (MyContext db=new MyContext)
{
  var query=db.Users.Where(u=>u.UserTypes.Any(t=>t.type_description==1));
}

if you need the UserTypes, then:

using (MyContext db=new MyContext)
{
  var query=db.Users
    .Include(u=>u.UserTypes)
    .Where(u=>u.UserTypes.Any(t=>t.type_description==1));
}

EF will abstract away the "UserTypes" cross reference table because you don't actually need it. All you should care about is what Types are associated with the Users. The fact there is a cross reference table helping you should be an implementation detail of how SQL works.

The navigational property could be called anything, and has relatively little to do with the name of the cross reference table. If the cross reference table was called say UserTypesCrossReferences, the navigation property could still be called UserTypes. You just so happen to have a table by what EF would want to call the navigation property by default.

Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • Hi Robert, I came up almost with your same solution one thing, I cannot reference u=>u.UserTypes.type_description==1, instead I need to put FirstOrDefault like UserTypes.FirstOrDefault().type_description==1. Moreover, there are results only for type_description==1 anything other than that gives me empty result. – dtjmsy May 17 '15 at 00:47
  • if I put type_description==2, this is the SQL that I got executed which returned me empty result each time: SELECT [Project2].[use_id] AS [use_id], [Project2].[use_name] AS [use_name], FROM ( SELECT [Extent1].[use_id] AS [use_id], [Extent1].[use_name] AS [use_name], (SELECT TOP (1) [Extent2].[ut_ut_id] AS [ut_ut_id] FROM [dbo].[UserType] AS [Extent2] WHERE [Extent1].[use_id] = [Extent2].[ut_use_id]) AS [C1] FROM [dbo].[User] AS [Extent1] ) AS [Project2] WHERE 2 = [Project2].[C1] even I got 2 types – dtjmsy May 17 '15 at 00:51
  • Oops. Updated answer. You need to check to see if any of the Types are ==1, so I've updated the queries to reflect that. – Robert McKee May 17 '15 at 00:59
  • Robert, That' s exactly what I was looking for, millions thanks, the lambda expressions code works pretty well in one line :), I usually use Ling to sql in my codes as an old boy :), is it possible to do the same thing in Linq to Sql somehow ? cheers – dtjmsy May 17 '15 at 01:15