0

I created a class within a class so it could be an alternative to writing 3 different methods calling to the database- so i created a class with related items and now when i try to build i get the error Cannot implicity convert type System.collections.generic.list<anonymousType#1> to class that i created StudentDocuments and when i click on the error it directs me to the return subs statement.

public class  StudentDocuments
    {
        public Guid DocID {get; set;}
        public string Assignment {get; set;}
        public DateTime Submitted {get; set;}
        public string Student {get;set;}
    }




public StudentDocuments GetStudentSubmissionGrid(Guid usr, Guid lib)
    {
        using (var dc = new DocMgmtDataContext())
        {
            var subs = (from doc in dc.Documents
                        join u in dc.Users on doc.OwnedByUserID equals u.ID
                        where doc.OwnedByUserID == usr && doc.LibraryID == lib
                        select new { 
                            DocID = doc.ID, 
                            Assignment = doc.Library.Name, 
                            Submitted = doc.UploadDT,
                            Student = u.FullName
                        })
                .OrderByDescending(c => c.Submitted).ToList();
            return subs;
        }
    }

1 Answer 1

3

You create anonymous type your LINQ. Instead you must create the specific type object list according your return type.

Use this:

select new StudentDocuments{ 
                               DocID = doc.ID, 
                               Assignment = doc.Library.Name, 
                               Submitted = doc.UploadDT,
                               Student = u.FullName
                           }

and declare your method as:

public List<StudentDocuments> GetStudentSubmissionGrid(Guid usr, Guid lib)

You can also return IEnumerable<StudentDocuments> instead of list.

UPDATE

var subs = (from doc in dc.Documents
                    join u in dc.Users on doc.OwnedByUserID equals u.ID
                    where doc.OwnedByUserID == usr && doc.LibraryID == lib
                    orderby doc.UploadDT descending
                    select new StudentDocuments
                     { 
                        DocID = doc.ID, 
                        Assignment = doc.Library.Name, 
                        Submitted = doc.UploadDT,
                        Student = u.FullName
                     }).AsEnumerable().ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks i think this will work but now it is telling me that i cannot implicity convert type iqueryable studentDocument to a list and that i might be missing a cast.
As an aside, you might want to think about having the GetStudentSubmissionGrid function return an IEnumerable, rather than returning a List. The benefit of this is that if you use LINQ further up to select a smaller list, it'll save some database work. (totally unrelated to answering the question, though).
@ColinDeClue advise considered and changed from List to IEnumerable thanks for explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.