หากคุณกำลังดำเนินการLinq to Entityคุณจะไม่สามารถใช้ปุ่มClassTypeด้วยnewในการselectปิดคิวรีonly anonymous types are allowed (new without type)
ลองดูตัวอย่างของโครงการของฉัน
//...
var dbQuery = context.Set<Letter>()
.Include(letter => letter.LetterStatus)
.Select(l => new {Title =l.Title,ID = l.ID, LastModificationDate = l.LastModificationDate, DateCreated = l.DateCreated,LetterStatus = new {ID = l.LetterStatusID.Value,NameInArabic = l.LetterStatus.NameInArabic,NameInEnglish = l.LetterStatus.NameInEnglish} })
^^ without type__________________________________________________________________________________________________________^^ without type
จากคุณเพิ่มnew keywordในการเลือกปิดแม้ว่าcomplex propertiesคุณจะได้รับข้อผิดพลาดนี้
ดังนั้นคำหลักในคำสั่ง ,,removeClassTypes from newLinq to Entity
เพราะมันจะเปลี่ยนเป็นคำสั่ง sql และดำเนินการบน SqlServer
ดังนั้นเมื่อฉันสามารถใช้new with typesเมื่อselectปิด?
คุณสามารถใช้มันหากคุณกำลังติดต่อกับ LINQ to Object (in memory collection)
//opecations in tempList , LINQ to Entities; so we can not use class types in select only anonymous types are allowed
var tempList = dbQuery.Skip(10).Take(10).ToList();// this is list of <anonymous type> so we have to convert it so list of <letter>
//opecations in list , LINQ to Object; so we can use class types in select
list = tempList.Select(l => new Letter{ Title = l.Title, ID = l.ID, LastModificationDate = l.LastModificationDate, DateCreated = l.DateCreated, LetterStatus = new LetterStatus{ ID = l.LetterStatus.ID, NameInArabic = l.LetterStatus.NameInArabic, NameInEnglish = l.LetterStatus.NameInEnglish } }).ToList();
^^^^^^ with type
หลังจากที่ฉันดำเนินการToListกับแบบสอบถามมันกลายเป็นin memory collection เพื่อให้เราสามารถใช้new ClassTypesในการเลือก