1

I need to create a string concatenation with an linq statement. I have the following data:

Section

  • Id: 1, Title: Test1
  • Id: 2, Title: Test2

Section_User

  • SectionId: 1, UserId: 1
  • SectionId: 1, UserId: 2

Users

  • Id: 1, Name: User1
  • Id: 2, Name: User2

I need the following result:

SectionId: 1, Users: User1, User2

I create the following Linq statement:

var query2 =    from section in this.context.Sections
                from users in section.Users
                group section by section2.Id into groupedSection
                select new {
                    SectionId = groupedSection.Key,
                    Users = string.Join(",", users.Select (x => x.Name)) // compile error, but I don't know how I write the statement correctly
                };

Could someone tell me, how I can create a string concatenation on database side (not in-memory) with an linq statement.

Thanks!!

4
  • 1
    Check this answer. Commented Jun 25, 2017 at 19:23
  • You can use string.format instead of join. Commented Jun 25, 2017 at 19:25
  • I don't think it's supported by EF. So unfortunately the answer is - you can't. Commented Jun 25, 2017 at 19:26
  • With EF you'll always have to do something like this. Commented Jun 25, 2017 at 20:44

1 Answer 1

0

Based on @Alexei 's link in the comments, your line should look like

var query2 =    from section in this.context.Sections
                from users in section.Users
                group section by section2.Id into groupedSection
                select new {
                    SectionId = groupedSection.Key,
                    Users = string.Join(",", (from u in users select s.Name).ToArray())
                };

However, if that doesn't work due to LINQ to Entities error, and you're willing to simply leave Users as an IEnumerable<string>, you can simply use:

var query2 =    from section in this.context.Sections
                from users in section.Users
                group section by section2.Id into groupedSection
                select new {
                    SectionId = groupedSection.Key,
                    Users = (from u in users select s.Name)
                };

And later whenever needed, you can join the strings, or display the names using a loop.

Sign up to request clarification or add additional context in comments.

2 Comments

The answer form Alexei's link does not work for me. Fo this I wrote the following answer: stackoverflow.com/a/44753791/2489386
I've searched for some time now, and there does not seem to by any way for your concat/join operation without ToList() or AsEnumerable(), the closest thing I could think of would be to write a pure MySQL statement and execute it using Raw SQL queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.