0

クラス内にクラスを作成したので、データベースを呼び出す3つの異なるメソッドを作成する代わりになる可能性があります-関連するアイテムを含むクラスを作成しましたが、ビルドしようとするとエラーが発生 Cannot implicity convert type System.collections.generic.list<anonymousType#1> to class that i created StudentDocuments し、エラーをクリックするとエラーが表示されますreturn subsステートメントに誘導します。

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

1 に答える 1

3

匿名型の LINQ を作成します。代わりに、戻り値の型に従って特定の型のオブジェクト リストを作成する必要があります。

これを使って:

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

メソッドを次のように宣言します。

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

IEnumerable<StudentDocuments>リストの代わりに返すこともできます。

アップデート

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();
于 2013-03-14T21:27:23.733 に答える