Controller
public JsonResult GetFee(int? Class_Id)
{
var list = Repository.FeeRepository.GetAll()
.Where(i => i.Class_Id == Class_Id)
.Select(x => new {x.tbl_SchoolProfile.Name, x.tbl_Classes.Name, x.tbl_FeeHead.Name, x.Amount})
.ToList();
return Json(list,JsonRequestBehavior.AllowGet);
}
I have problem to access the field of other tables which is tbl_Classes , tbl_FeeHead is join with this fee table in FeeRepository.GetAll() i select all the data from tbl_fee it give compilation error (An anonymous type cannot have multiple properties with same name) so how can i fix this error ?
You need to specify the names you want in the anonymous object creation expression, e.g.
.Select(x => new {
SchoolName = x.tbl_SchoolProfile.Name,
ClassName = x.tbl_Classes.Name,
FeeHeadName = x.tbl_FeeHead.Name,
x.Amount
})
You can only leave the name out if the compiler will infer the right one from the value you're assigning, e.g.
new { Foo = x.Foo, Bar = y.Bar }
is equivalent to
new { x.Foo, y.Bar }
See more on this question at Stackoverflow