Click here to Skip to main content
16,011,957 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have following Linq Query in Controller
ViewBag.data=(from cg in db.Colleges
                           from tn in db.Towns
                           from ds in db.Districts
                           where cg.CgTnCode ==tn.TnCode && tn.TnDscode==ds.DsCode 
                           orderby ds.DsCode,tn.TnCode,cg.CgCode
                   select new
                   {
                       cg.CgCode,
                       cg.CgShName,
                       cg.CgName,
                       cg.CgType,
                       DsCode = ds.DsCode,
                       DsName=ds.DsName,
                       TnCode= tn.TnCode,
                       TnName=tn.TnName,
                       cg.CgAddr1,
                       cg.CgAddr2 ,cg.CgAddr3,cg.CgLoca,cg.CgPin,cg.CgPhone,cg.CgFax,cg.CgEmail ,cg.CgWeb,cg.CgOnlyCent});


I want to display all selected fields in View..
I have written code in view to acess ViewBag.data as follows :
@foreach(var entry in ViewBag.data)
             {
              @entry.CgCode
              @entry.CgShName
              @entry.CgName
              @entry.CgTnCode
              @entry.DsCode
             }

Its Giving Error as does not contain a definition for 'DsCode'
what should I do can anyone help??
Posted
Updated 4-Sep-13 1:49am
v2

1 solution

from the view cast to IEnumerable of Enity like below
C#
@foreach(var entry in (IEnumerable<YourEntityClass>)ViewBag.data)

Hope this helps
 
Share this answer
 
Comments
priyankavm 4-Sep-13 8:37am    
Thanks.....But its not working
becoz I have 3 different Entity classes
Jameel VM 4-Sep-13 10:28am    
Create a new class and put all the properties to that class. and change your linq like select new YourNewClass{
//set all the properties here.
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900