I do have have a query as below, and i would like to display them into my view as code below also. but then i get some error. Please advice. Thanks in advance
@foreach(var item in ViewBag.Accommodation)
{
@item.ToString()
@item.id <-- this is not working (the error say it is null )
}
My Result as below
{ id = 1, name = Century Bay Residence, gmap = http://maps.google.com/maps?q=e-gate&hl=en&ll=3.141802,101.629972&spn=0.075332,0.132093&sll=4.854099,100.737575&sspn=0.300698,0.528374&hq=e-gate&radius=15000&t=m&z=14&iwloc=A, address = Sunrise Tower, 32, Jalan Eden, 12540 Penang, Malaysia, picture = /images/Accommodation/948f31c3-c5df-4d2f-8b74-316a5a361576.jpg }
I though i have show you the string output at the post?? I have the attribute ID and it has a value attached with it.. i just want to loop them out.. but i got the error.. so could you be more understand on what i mean? Thanks
I have tried with just Distinct, it is working, but then the data is not what i wanted, so i just select the few column which i just want.
The issue is because of you add distinct. let say you have two value
1 Test true
2 Test true
So here if you apply distinct then it will give you only one row but which is the id its 1 or 2 so it is the issue, so first you will decide which ID do you need? then based on that you can set your logic.
My Tech Blogs MCPD Enterprise and Web Application
MCTS Web, Window and Enterprise Application
I have tried with just Distinct, it is working, but then the data is not what i wanted, so i just select the few column which i just want.
The issue is because of you add distinct. let say you have two value
1 Test true
2 Test true
So here if you apply distinct then it will give you only one row but which is the id its 1 or 2 so it is the issue, so first you will decide which ID do you need? then based on that you can set your logic.
Sorry i got read my error message wrongly..
when i just call @item.id .. it give me error as below
'object' does not contain a definition for 'id'
but then when i call @item.toString(). it give me a string as below
{ id = 1, name = Century Bay Residence, gmap = http://maps.google.com/maps?q=e-gate&hl=en&ll=3.141802,101.629972&spn=0.075332,0.132093&sll=4.854099,100.737575&sspn=0.300698,0.528374&hq=e-gate&radius=15000&t=m&z=14&iwloc=A, address = Sunrise Tower, 32, Jalan Eden, 12540 Penang, Malaysia, picture = /images/Accommodation/948f31c3-c5df-4d2f-8b74-316a5a361576.jpg }
BleuSnow
Member
87 Points
80 Posts
Entity Database + Select Column
Apr 17, 2012 06:04 PM|LINK
Hi All,
I do have have a query as below, and i would like to display them into my view as code below also. but then i get some error. Please advice. Thanks in advance
ViewBag.Accommodation = db.tbl_price.Where(t => t.tbl_accommodation.tbl_location.id == location).Where(t => t.start_date < DateTime.Now && t.expire_date > DateTime.Now).Distinct().Select(t => new { t.id, t.tbl_accommodation.name, t.tbl_accommodation.gmap, t.tbl_accommodation.address, t.tbl_accommodation.picture });@foreach(var item in ViewBag.Accommodation) { @item.ToString() @item.id <-- this is not working (the error say it is null ) }My Result as below
{ id = 1, name = Century Bay Residence, gmap = http://maps.google.com/maps?q=e-gate&hl=en&ll=3.141802,101.629972&spn=0.075332,0.132093&sll=4.854099,100.737575&sspn=0.300698,0.528374&hq=e-gate&radius=15000&t=m&z=14&iwloc=A, address = Sunrise Tower, 32, Jalan Eden, 12540 Penang, Malaysia, picture = /images/Accommodation/948f31c3-c5df-4d2f-8b74-316a5a361576.jpg }ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: Entity Database + Select Column
Apr 18, 2012 04:38 AM|LINK
Maybe it is . Verify for null (@if(item.id != null) )
BleuSnow
Member
87 Points
80 Posts
Re: Entity Database + Select Column
Apr 18, 2012 06:28 AM|LINK
Hi brother,
I though i have show you the string output at the post?? I have the attribute ID and it has a value attached with it.. i just want to loop them out.. but i got the error.. so could you be more understand on what i mean? Thanks
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: Entity Database + Select Column
Apr 18, 2012 07:26 AM|LINK
first, I am not your brother.
if it has, it would not give you null. Please show whole error stack trace.
amitpatel.it
Star
8070 Points
1880 Posts
Re: Entity Database + Select Column
Apr 18, 2012 07:33 AM|LINK
There are two posiblitlites,
1. Might be id is available null in database
2. Creating an issue because of distinct, so remove it and check it.
MCPD Enterprise and Web Application
MCTS Web, Window and Enterprise Application
BleuSnow
Member
87 Points
80 Posts
Re: Entity Database + Select Column
Apr 18, 2012 10:09 AM|LINK
Hi, I the issue is coming from the Select..
I have tried with just Distinct, it is working, but then the data is not what i wanted, so i just select the few column which i just want.
and most important is the ID is not null.. it is my PK.. how can it be null and if it yes, how u explain my output string?
ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: Entity Database + Select Column
Apr 18, 2012 10:57 AM|LINK
please show the whole error stack trace.
amitpatel.it
Star
8070 Points
1880 Posts
Re: Entity Database + Select Column
Apr 18, 2012 11:29 AM|LINK
The issue is because of you add distinct. let say you have two value
1 Test true
2 Test true
So here if you apply distinct then it will give you only one row but which is the id its 1 or 2 so it is the issue, so first you will decide which ID do you need? then based on that you can set your logic.
MCPD Enterprise and Web Application
MCTS Web, Window and Enterprise Application
BleuSnow
Member
87 Points
80 Posts
Re: Entity Database + Select Column
Apr 18, 2012 02:27 PM|LINK
Sorry i got read my error message wrongly..
when i just call @item.id .. it give me error as below
'object' does not contain a definition for 'id'
but then when i call @item.toString(). it give me a string as below
{ id = 1, name = Century Bay Residence, gmap = http://maps.google.com/maps?q=e-gate&hl=en&ll=3.141802,101.629972&spn=0.075332,0.132093&sll=4.854099,100.737575&sspn=0.300698,0.528374&hq=e-gate&radius=15000&t=m&z=14&iwloc=A, address = Sunrise Tower, 32, Jalan Eden, 12540 Penang, Malaysia, picture = /images/Accommodation/948f31c3-c5df-4d2f-8b74-316a5a361576.jpg }ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: Entity Database + Select Column
Apr 18, 2012 02:31 PM|LINK
That;s other error as the first one!
For this, create a class named MyClassWithDatas , that have
public int id {get;set;}
and
Select(t => new MyClassWithDatas () { id = t.id , ...