MVC - Linq - <T> popup list with entries in another table

I am prototyping my first MVC application, it is a simple forum. I made part of a domain model, and I'm trying to figure out how to make something very simple in SQL, but I can't figure it out in my application. Here are my objects:

[Table(Name="Users")]
public class User
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    public int Id { get; set; }

    [Column] public string Username { get; set; }
    [Column] public string Password { get; set; }
    [Column] public string PasswordHash { get; set; }
    [Column] public string PasswordSalt { get; set; }
    [Column] public string FirstName { get; set; }
    [Column] public string LastName { get; set; }
    public List<Forum> AllowedForums { get; set; }
    [Column] public DateTime LastLogin { get; set; }
    [Column] public DateTime MemberSince { get; set; }
}

[Table(Name="Forums")]
public class Forum
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    public int Id { get; set; }
    [Column] public int ParentId { get; set; }
    [Column] public string Title { get; set; }
    [Column] public string Description { get; set; }
    [Column] public bool IsGlobal { get; set; }
    [Column] public int DisplayOrder { get; set; }
}

I also have an AllowedForums link table that looks like this:

userid  forumid
  1       4

To select the forums that the user is allowed to view, and the forums where IsGlobal == true, I would do this in SQL:

SELECT * FROM Forums
LEFT OUTER JOIN AllowedForums ON Forums.id = AllowedForums.Forumid
WHERE AllowedForums.Userid = 1
OR Forums.IsGlobal = 1

How do i fill

public List<Forum> AllowedForums

using C # / Linq to SQL?

AllowedForum ? , . EntitySet, , , . , , . BTW, # OO. , , , / , , .

+3
2

Entity (, ), AllowedForums . , User PK/FK AllowedForums. , User , :

internal EntitySet<AllowedForums> AllowedForumsRelationships
{
   get;set;
}

- . , . AllowedForums . . ( LINQ to SQL, ).

, , - :

    public IList<Forum> AllowedForums
    {
       get
       {
          var result = new List<Forum>();
          foreach(var relationShip in this.AllowedForumsRelationships)
          {
             result.Add(relationShip.Forum);
             return result;
          }
       }
    }

- , , , 100% , , . , .

EDIT: Northwind :

: , . , :

public partial class Order
{
        public IList<Product> Products
        {
            get
            {
                var list = new List<Product>();
                foreach (var item in this.Order_Details)
                {
                    list.Add(item.Product);
                }
                return list;
            }
        }
 }

, , .

+3

- :

var AllowedForums = from f in ForumsTable
                    join af in AllowedForumsTable on f.Id equals af.forumid into temp
                    from aft in temp.DefaultIfEmpty()
                    where (f.IsGlobal == 1 || aft.userid == 1)
                    select f;
0

Source: https://habr.com/ru/post/1718049/


All Articles