【问题标题】:Linq to SQL ClassLinq to SQL 类
【发布时间】:2008-12-29 20:47:37
【问题描述】:

我有一个类见下文,当我试图从另一个页面查询它时它没有链接,例如如果我输入“var Notes = HandhedNotes.GetNotesByLinkIDJoin();”当我查看注释时,那里没有链接。我认为这是由于 IQuerable 也许我应该使用其他的东西??

namespace WebSys
{
  public partial class HandheldNote
  {        
    public static IQueryable GetNotesByLinkIDJoin()
    {
        WebSysDataContext db = new WebSysDataContext(Contexts.WEBSYS_CONN());

        var Note = from x in db.HandheldNotes
                   join t in db.Employees on x.By equals t.EmployeeNo
                   orderby x.DateAdded
                   select new
                   {
                       DateRaised = x.DateAdded,
                       Note = x.Note,
                       TypeID = x.TypeID,
                       EmployeeID = t.Forenames + " " + t.Surname
                   };
        return (IQueryable)Note;
    }

}

}

【问题讨论】:

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


    【解决方案1】:

    任何你需要的。
    看来您实际上是在尝试收集笔记,因此您可以这样做...

    public IList<Note> GetMyNotes()
    {
    IList<Note> NoteList = (from x in db.HandheldNotes
                       join t in db.Employees on x.By equals t.EmployeeNo
                       orderby x.DateAdded
                       select new Note
                       {
                           DateRaised = x.DateAdded,
                           Note = x.Note,
                           TypeID = x.TypeID,
                           EmployeeID = t.Forenames + " " + t.Surname
                       }).ToList<Note>();
            return NoteList;
    }
    

    【讨论】:

      【解决方案2】:

      你没有列举任何东西。延迟加载是最好的。访问 Note 上的匿名参数之一,您将能够访问它们。

      你还可以做的是创建类...

      public class Note
      {
      public DateTime DateRaised;
      public string Note ;
      public int TypeID;
      public string EmployeeID;
      }
      

      然后

      Note Note = (from x in db.HandheldNotes
                         join t in db.Employees on x.By equals t.EmployeeNo
                         orderby x.DateAdded
                         select new Note
                         {
                             DateRaised = x.DateAdded,
                             Note = x.Note,
                             TypeID = x.TypeID,
                             EmployeeID = t.Forenames + " " + t.Surname
                         }).FirstOrDefault();
              return Note;
      

      我认为这样说很重要:

      您正在创建 DataContext,然后尝试返回尚未枚举的 LINQ 变量。当您尝试访问此信息时,您将收到底层数据上下文不存在的错误。您的 2 个选项是在其他地方使用 Datacontext 或从此函数返回枚举值。使用 ToArray() 或 ToList() 或类似的东西...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多