1

I have a Person Entity something like that

PersonId
PersonName
PersonPhone

I have a House Entity

HouseID
HouseType
HouseSize

Both Entities are related with many to many relation. I need to have all the HouseType for a person (which has many houses) into a string.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
Kulpemovitz
  • 531
  • 1
  • 9
  • 23

3 Answers3

0

You can easily concatenate multiple strings from a sequence with the Enumerable.Aggregate method.

In your case you'll have to first project the list of House entities into a list of House.HouseType strings and then aggregate them into a single string:

var houseTypes = person.Houses.Select(i => i.HouseType).AsEnumerable();
var emptyResult = "0";

return houseTypes.Any() ?
    houseTypes.Select(i => i.ToString())
              .Aggregate((current, next) => current + ", " + next) :
    emptyResult;

Alternatively you could simply say:

var houseTypes = person.Houses
                       .Select(i => i.HouseType)
                       .AsEnumerable()
                       .Select(i => i.ToString());
return String.Join(", ", houseTypes);

which will return an empty string when the houseTypes sequence is empty.

Update:

If you're using Entity Framework 4 or above, you can use one of the built-in SQL functions to perform the conversion to string directly in the database:

var houseTypes = person.Houses
                       .Select(i => SqlFunctions.StringConvert((double)i.HouseType))
                       .AsEnumerable()
return String.Join(", ", houseTypes);
Community
  • 1
  • 1
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • My houseType is not a string so I get an Error "Cannot implicity convert type 'string' to 'short'" – Kulpemovitz Feb 02 '12 at 13:32
  • @Kulpemovitz If the `HouseType` property is of type short you can just convert to a string with `ToString()`. I updated my example. – Enrico Campidoglio Feb 02 '12 at 13:49
  • Great.But now it fails if the person has no house(s). but I still need the person in the result – Kulpemovitz Feb 02 '12 at 13:57
  • what do you think? person.Houses.Count > 0 ? vs.person.Select(i => i.HouseType.ToString()).Aggregate((current, next) => current + ", " + next) : "0" – Kulpemovitz Feb 02 '12 at 14:04
  • @Kulpemovitz You're most likely getting exception stating that the `Sequence contains no elements` when the `Select` method returns an empty sequence. I updated my answer. – Enrico Campidoglio Feb 02 '12 at 14:27
  • I will stay with my syntax. Thank you – Kulpemovitz Feb 02 '12 at 14:34
  • This doesn't work in linq to entities (both .ToString and .Aggregate can't be translated). It does work in linq to objects, but that's not what the question is tagged as. – John Jan 16 '14 at 19:04
0

You mean something like?:

var data = context.People.Where(p => p.PersonId == <id>).SelectMany(p => p.Houses).Select(h => h.HouseType);
var result = string.Join(<separator>, data);
cincura.net
  • 4,130
  • 16
  • 40
0
var houseTypes = person.Houses
    .Select(i => i.HouseType).ToList();

return string.Join(" ", houseTypes.Select(x=>x.ToString()));
Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83