0

This is my property which i am returning back from a anonymous function:

var ret = new
{
    TagName = String.Join( ",", post.Tags.Select( t => t.TagName ) )
};

Here, post have many-to-many relationship with tag means a post can have multiple tags.

I want to null check here, if there is any tag then return back comma separated otherwise return empty string.

So far, I was trying many ways to null check like this but not getting exact syntax:

 TagName = String.Join( ",", post.Tags.Select( t => t.TagName ) + ( post.Tags != null ? '' ) )

in case of simple string, i can get it like this:

alert( post.username != null ? ' ' + post.username : '') )
Dai
  • 141,631
  • 28
  • 261
  • 374
duke
  • 1,816
  • 2
  • 18
  • 32

4 Answers4

3

Try something like this

TagName = post.Tags != null ? String.Join(",", post.Tags.Select( t => t.TagName ) ) : ""

and check this link about trensry operator https://msdn.microsoft.com/en-us/library/zakwfxx4(v=vs.90).aspx

suvroc
  • 3,058
  • 1
  • 15
  • 29
1

If I understand you correctly, you want to concatenate tags, but if there are no tags then return an empty string? I also assume you want to ignore null (or empty) members of post.Tags too?

var ret = new {
    TagName = ( post.Tags == null || post.Tags.Length == 0 ) ?
        String.Empty :
        String.Join( ",",
            post.Tags
                .Where( t => t != null && !String.IsNullOrWhiteSpace( t.TagName ) )
                .Select( t => t.TagName )
        )
};
Dai
  • 141,631
  • 28
  • 261
  • 374
1

Based on the solution in this related post. You could do this

var ret = new
{
    var tags = post.Tags.OrEmptyIfNull();
    TagName = String.Join( ",", tags.Select( t => t.TagName ) )
};

The OrEmptyIfNull is basically a wrapper around the null coalescence operator ?? but I find it easier to read that the exploded version. Since String.Join will produce an empty string if the collection is empty you can simply pass tags in, knowing that it will never been null

Community
  • 1
  • 1
Rune FS
  • 21,497
  • 7
  • 62
  • 96
  • thnks for the reply, one ques--on performance wise, this one line code will be faster or urs i dont know much so just asking @Rune Fs TagName = post.Tags != null ? String.Join(",", post.Tags.Select( t => t.TagName ) ) : "" – duke Jan 14 '16 at 08:50
  • I'd say it's highly likely that a) The difference will be hard to detect for the user b) It can likely be optimized to the same binary code by the optimizer. Aka it will result in the same or at least very similar set of machine instructions with no difference in execution time. In general prefer readability to performance. If there's a measured performance problem you can solve that. However readable code mean more productive teams. More productive teams have more time to optimize where optimization is indeed valuable to the product – Rune FS Jan 14 '16 at 10:55
1

You could use something like this :

var test = {
     TagNames= post.Tags!=null && post.Tags.Any() ? String.Join(",", post.Tags.Select(x=>x.TagName) : ""
}
Cosmin
  • 2,184
  • 21
  • 38