tags:

views:

69

answers:

1

Wondering if I can conditionally concatenate LINQ query results from 4 different fields in a table, which are flags into 1 string for each record, so it would be something like this:

            dbContext.CONTACT_LOGs.Select( e => new 
            (e => new ContactLog
            {
                rec1 = e.Recommend1 ? "1 is recommended" : String.Empty,   
                rec2 = e.Recommend2 ? "2 is recommended" : String.Empty,
                rec3 = e.Recommend3?  "3 is recommended" : String.Empty,
                rec4 = e.Recommend4 ? "4 is recommended" : String.Empty
                //this doesn't work in a single query
                ContactSummary = String.Concat(rec1, rec2, rec3, rec4)
            });


            public class ContactLog
            {
            public string rec1 { get; set; }
            public string rec2 { get; set; }
            public string rec3 { get; set; } 
            public string rec4 { get; set; } 
            public string ContactSummary { get; set; }


    }

Is there any way to do that in that fashion? Thanks

+1  A: 

I'm not sure if I understand your question, but I would just add a new property to your ContactLog class as follows:

public class ContactLog {
   public string rec1 { get; set; }
   public string rec2 { get; set; }
   public string rec3 { get; set; } 
   public string rec4 { get; set; } 
   public string ContactComments { get; set; }
   public string ContactLog {
      get {
         return String.Concat(rec1, rec2, rec3, rec4);
      }
   }
}

No need to burden the linq query with that

Marek Karbarz
That seems to do what I need, but the only problem, as you can see from above snippet, sometimes they can be empty and in that case I want to exclude them from concatenation, but if all flags are true, I need to get "1,2,3,4 recommended" in that summary string
VictorS