-1

I need to concatenate 4 fields and display as a single field using | separators. I have tried string.Concat() but it didn't work. Also tried to use string.Join() but can't use it.

var result = dbContext.MyEntity
                      .Where(me=> me.field1!= null && me.field2 != null)
                      .Select(me => new MyViewModelClass()
                                        {
                                           Field1 = me.field1,
                                           Field2 = me.field2,
                                           Field3 = me.field3,
                                           Field4 = me.field4,
                                           Field5 = me.field5,
                                           Field6 = me.field6,    
                                           // I need to concatenate field3, field4, field 5, field 6 by "|" separator.                 
                                           Field = string.Concat("|", me.field3, me.field4, me.field5, me.field6)
                                       });
return result;

Any suggestions?

Thanks,

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user659469
  • 325
  • 1
  • 7
  • 22

3 Answers3

4

You would need to put a .ToList() in between your Where and Select clauses, so that LINQ will use the LINQ-to-Objects provider (which can use C# string methods) instead of the LINQ-to-Entities provider (which can't, since it's trying to translate that into a SQL query).

Your code would then be like this:

var result = dbContext.MyEntity
             .Where(me=> me.field1 != null && me.field2 != null)
             .ToList()
             .Select(me => 
                new MyViewModelClass()
                {
                  Field1 = me.field1,
                  Field2 = me.field2,
                  Field3 = me.field3,
                  Field4 = me.field4,
                  Field5 = me.field5,
                  Field6 = me.field6,    
                  Field = string.Concat("|", me.field3, me.field4, me.field5, me.field6)
                }
              );
Corey Adler
  • 15,897
  • 18
  • 66
  • 80
1

UPDATE Please follow what @IronMan84 has suggested and apply .ToList(). In addition if you want to be more efficient, you can use string.format

Field = string.Format("{0}|{1}|{2}|{3}",me.field3,me.field4,me.field5,me.field6);

If you are using c# 6.0 (.NET Framework >= 4.6.1), then you can use

Field = $("{me.field3}|{me.field4}|{me.field5}|{me.field6}");
Vinod
  • 1,882
  • 2
  • 17
  • 27
0
   var result= dbContext.MyEntity.Where(me=> me.field1!= null && me.field2 != null).Select(me=>
        new MyViewModelClass()
        {
            Field1= me.field1,
            Field2= me.field2,
            Field3= me.field3,
            Field4= me.field4,
            Field5= me.field5,
            Field6= me.field6,    

            //I need to concatenate field3, field4, field 5, field 6 by "|" separator.                 

            Field=  string.Concat("|",me.field3).Concat("|")
                          .Concat(field4).Concat("|")
                          .Concat(field5).Concat("|")
                          .Concat(field6)

        });
    return result;
Auguste
  • 2,007
  • 2
  • 17
  • 25