-3

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.

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]

What am I doing wrong, and how do I get the Name property string?

Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
user3066571
  • 1,381
  • 4
  • 14
  • 37
  • 1
    Possible duplicate of [IEnumerable to string](https://stackoverflow.com/questions/3414263/ienumerable-to-string) – mjwills Aug 12 '18 at 21:33
  • 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 '18 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 '18 at 21:41
  • Try `string field = Model.FormElementTypes.Where(f => f.ID == e.FormElementTypesID).Select(f => f.Name).FirstOrDefault()`; – mjwills Aug 12 '18 at 21:42

3 Answers3

1

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();
                }
            }
        }
Or Yaacov
  • 3,597
  • 5
  • 25
  • 49
  • 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 '18 at 21:40
  • @user3066571, according to your code it is not a single entity but a collection of entitites. – Cetin Basoz Aug 12 '18 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 Where( this IEnumerable source, Func predicate ) returns a IEnumerable, which is a collection base type: https://msdn.microsoft.com/en-us/library/system.collections.ienumerable(v=vs.110).aspx – Or Yaacov Aug 12 '18 at 21:52
0

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>();
}
sayah imad
  • 1,507
  • 3
  • 16
  • 24
-1

You meant to call ToList() rather than ToString() to execute the query

 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();
Rahul
  • 76,197
  • 13
  • 71
  • 125