views:

194

answers:

4

Hi,

I have a Class see below, when i am trying to query it from another page it not linking e.g. if i put "var Notes = HandhedNotes.GetNotesByLinkIDJoin();" when i look at Notes theres not links there. I think its due to 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;
    }

}

}

+1  A: 

you have not enumerated over anything. Lazy loading at it's finest. access one of the anonymous parameters on Note and you will be able to access them all.

what you could also do is create the 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 is important to say this:

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

E Rolnicki
A: 

Not sure what that means can you expand on that please?

by not actually forcing LINQ to enumerate over the anonymous class you projected into. unless you use one of the "greedy" operators, you will allow LINQ to defer the execution(it will keep track of HOW to get what you want, rather than what you want)
E Rolnicki
A: 

What would I wrap the "Note Note =" block in public void??

+1  A: 

whatever you need.
it appears that you are actually trying to get a collection of notes, so you could 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;
}
E Rolnicki