Selecting ToString() on a String object does not return the actual String
Clash Royale CLAN TAG#URR8PPP
Selecting ToString() on a String object does not return the actual String
I'm using MVC, and one of my models is FormElementTypes
, which has an int ID
and string Name
. When I'm attempting to select the Name
property with Lambda, it doesn't return the actual string of the property.
FormElementTypes
int ID
string Name
Name
example
foreach (FormElements e in Model.FormElements)
string field = Model.FormElementTypes.Where(f => f.ID == e.FormElementTypesID).Select(f => f.Name).ToString();
The Name
property I should be selecting should have the value of text
. But when I write it out it spits: System.Linq.Enumerable+WhereSelectListIterator`2[Scholar.Models.FormElementTypes,System.String]
Name
text
System.Linq.Enumerable+WhereSelectListIterator`2[Scholar.Models.FormElementTypes,System.String]
What am I doing wrong, and how do I get the Name
property string?
Name
That is perfectly right. You are not using ToString() on Name property, you are using it on a .Select() which is System.Linq.Enumerable ...
– Cetin Basoz
Aug 12 at 21:41
@mjwills possibly? The syntax and what they were doing is so different than mine that I wouldn't say so, even if the issue under the hood could technically be the same. Given that I'm not very good at this, I need to operate off of context that is similar to what I'm doing.
– user3066571
Aug 12 at 21:41
Try
string field = Model.FormElementTypes.Where(f => f.ID == e.FormElementTypesID).Select(f => f.Name).FirstOrDefault()
;– mjwills
Aug 12 at 21:42
string field = Model.FormElementTypes.Where(f => f.ID == e.FormElementTypesID).Select(f => f.Name).FirstOrDefault()
3 Answers
3
this is happening because you are invoking ToString() on an Enumerable object
which is a collection object.
You will need to iterate over the collection.
you can read more about the where function in Linq in here
foreach (FormElements e in Model.FormElements)
var fields = Model.FormElementTypes.Where(f => f.ID == e.FormElementTypesID);
if (fields != null)
foreach (var field in fields)
string s = field.Name.ToString();
I see, so even though it's a single entity, I still need to iterate over this. I will try this out and report back, thanks for the help!
– user3066571
Aug 12 at 21:40
@user3066571, according to your code it is not a single entity but a collection of entitites.
– Cetin Basoz
Aug 12 at 21:49
you are welcome and I'll wait to hear the report but what's important to understand is that it's not a single entity, the system.Linq method signature public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, int, bool> predicate ) returns a IEnumerable, which is a collection base type: msdn.microsoft.com/en-us/library/…
– Or Yaacov
Aug 12 at 21:52
it normal to have this probleme beaucause you try to convert an IEnumerable to String which is impossible .
best regards .
please try this code :
foreach (FormElements e in Model.FormElements)
string field = Model.FormElementTypes.Where(f => f.ID == e.FormElementTypesID).Select(f => f.Name).FirstOrDefault<string>();
You meant to call ToList()
rather than ToString()
to execute the query
ToList()
ToString()
var model = Model.FormElementTypes.Where(f => f.ID == e.FormElementTypesID).Select(f => f.Name).ToList();
Just get the entity like
var modelName = Model.FormElementTypes.Where(f => f.ID == e.FormElementTypesID)
.Select(f => f.Name)
.FirstOrDefault();
You cannot assign a
List<T>
to a string
– series0ne
Aug 12 at 21:39
List<T>
string
@series0ne obviously not and so pointed that he probably wants to just execute the query
– Rahul
Aug 12 at 21:40
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of IEnumerable to string
– mjwills
Aug 12 at 21:33