除了創建一個新的未命名對象這一事實外,linq 表達式的細節并不重要: select new { ... }因此我沒有函數的返回類型。必須有一個通用的無類型類型,比如 Javascript 的“any”。 public ??? Get(int id) { var hisGrade = (from p in ctx.Students where p.StudentID == id select new { area = p.Grade.Section, name = p.Grade.GradeName }).FirstOrDefault(); return hisGrade; }我找到的每個示例都只顯示沒有封閉函數的代碼!
2 回答

守著星空守著你
TA貢獻1799條經驗 獲得超8個贊
您是否假設它始終必須是 Select 調用返回的匿名類型?你錯了。
public struct MyType
{
public string Area { get; set; }
public string Name { get; set; }
}
class Class1
{
public MyType Get(int id)
{
var hisGrade = (from p in ctx.Students
where p.StudentID == id
select new MyType{ Area = p.Grade.Section, Name = p.Grade.GradeName }).FirstOrDefault();
return hisGrade;
}
}
- 2 回答
- 0 關注
- 202 瀏覽
添加回答
舉報
0/150
提交
取消