2017-01-15 3 views
0

This is the structure of the database on which i am working.Click on this text.はasp.netのMVCでは、多くの関係に

を多く使用してデータベースからレコードを取得する私は、列MailStatusがFalse含まれMailStatusTableで述べた特定の区分 に属するユーザーのEMAILIDを選択します。

これは私がこれまで持っているクエリです:

var category = from m in mcontext.MailStatusTable 
          where m.MailStatus == false 
          select new 
          { 
           CategoryID = m.CategoryID 
          }; 
      var email_list = from u in mcontext.UserProfiles 
          where u.AnExpert == true 
          where u.AnInstitute == true 
          where category.Contains(u.UserProfileID) 

          select new 
          { 
           LastName = u.LastName, 
           EmailU = u.Email 
          }; 

しかし、私の第三USERPROFILES表とカテゴリ表の両方が接続されているなどの条件がサード表を経由して間違っていると、そのテーブルを持っていない場合その中の両方の列としてのモデルクラスは外部キーです。

これは私が私のコンテキストクラスで定義されている関係である:

modelBuilder.Entity<Categories>() 
      .HasMany(c => c.CategoriesUserProfile).WithMany(x => x.UserProfileCategories).Map(q => q.MapLeftKey("CategoryID").MapRightKey("UserProfileID").ToTable("UserProfileCategories")); 

どのように私は、特定のカテゴリに属する​​ユーザーのEMAILIDを選択する必要があります。

ありがとうございます。 確かに、 これらは私のモデルです:

public class UserProfiles 
{ 
    public int UserProfileID { get; set; } 
    public string LastName { get; set; } 
    public bool AnExpert { get; set; } 
    public bool AnInstitute { get; set; } 
    public bool Client { get; set; } 
    public string Email { get; set; } 
    public virtual ICollection<Categories> UserProfileCategories{get;set;} 
} 
public class Categories 
{ 
    public int CategoryID { get; set; } 
    public String Name { get; set; } 
    public virtual ICollection<UserProfiles> CategoriesUserProfile { get; set; } 
    public virtual ICollection<MailStatusTables> CategoriesMailStatusTable { get; set; } 

} 
public class MailStatusTables 
{ 
    public int MailStatusID { get; set; } 
    public bool MailStatus { get; set; } 
    public int EnquiryID { get; set; } 
    public int CategoryID { get; set; } 
    public virtual Categories MailStatusCategory { get; set; } 

} 
+0

あなたのモデルを投稿できますか? –

答えて

0

LINQクエリを書くとき、それはあなたがクエリが返すようにしたいエンティティ(または、あなたがしたいプロパティが含まれているものから始めるのが最適に動作時間のほとんど戻る)。したがって、この場合には、UserProfileは:

var email_list = from u in mcontext.UserProfiles 
       where u.AnExpert 
        && u.AnInstitute 
        && u.UserProfileCategories 
         .Any(c => c.MailStatusTables 
            .Any(ms => !ms.MailStatus)) 
       select new 
       { 
        LastName = u.LastName, 
        EmailU = u.Email 
       }; 

これは、少なくとも1つの非メールのステータスを含む少なくとも1つのカテゴリを持っているユーザーから、あなたにすべてのユーザーデータが返されます。

ちなみに、私はIsExpertまたはHasMailStatusのようなプロパティ名がブール値であることを伝えるので、プロパティ名を好むでしょう。