Linq to SQL:从许多表创建复杂对象

时间:2013-10-25 14:27:26

标签: c# sql linq linq-to-sql

我想知道如何获得一个物体。

我有3张桌子:

Categories -
 -- Category_ID
 -- Category_Deleted
 -- Category_Date_Creation

Languages
 -- Language_ID
 -- Language_Code
 -- Language_Name

Category_Names
 -- Category_ID
 -- Language_ID
 -- Category_Name_Value

从这些表中我创建了一个类:

 public class Language
 {
      public int Id;
      public string Code;
      public string Name;

      public static Language Covert(DataContext.Language lang)
      {
           return new Language() { Id = lang.Id, Code = lang.Code, Name = lang.Name }
      }
 }

 public class Catigory
 {
      public int Id;
      public DateTime DateCreation;
      public Dictionary<Language, string> Names;
 }

现在我这样做:

 Catigory cat = (from catigory in DataContext.Categories where catigory.Id == ID 
      select new Catigory() { Id = catigory, DateCreation = catigory.DateCreation }).First();
 cat.Names = (from cn in DataContext.Category_Names 
             join l in DataContext.Languages on cn.Language_ID equals l.Language_ID
             where cn.Category_ID == cat.Id
             select new { Key = l, Val = cn.Category_Name_Value }).ToDictionary(ss => Language.Covert(ss.Key), ss => ss.Val);

所以我想从Linq创建一个Category对象,只能通过一个查询。

0 个答案:

没有答案