Linq to SQL Class

I have a class, see below, when I try to request it from another page, it doesn't bind, eg. if i put "var Notes = HandhedNotes.GetNotesByLinkIDJoin ();" when I look at Notes there are no links there. I think because of the IQuerable, maybe I should be using something elese ??

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;
    }

}

      

}

0


source to share


2 answers


you have not listed anything. Lazy loading is the best. access to one of the anonymous parameters in Note and you will be able to access them.

what you can also do is create a class ...

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

      

and then



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;

      

I think it's important to say:

you are creating a DataContext and then trying to return a LINQ variable that was not listed. You will get an error that the underlying datacontext does not exist when you try to access this information. your 2 variants should either have Datacontext elsewhere or return the specified value from this function. use ToArray () or ToList () or something similar ...

+1


source


what you need.
it seems like you are actually trying to get a collection of notes so you can do this ...



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;
}

      

+1


source







All Articles