0

So, we are trying to display information using a Linq statement yet the issue we are having is we don't want some elements to be created if a variable is "" - Currently we are unable to do this as we cannot include an 'if' statement within the linq statement. How would we get around this; the code we have is listed below.

(E.g - We don't want the 'x.Phone' element to show if it is set to "")

    Root = new RootElement ("Student Guide") {
        new Section("Contacts"){
            from x in AppDelegate.getControl.splitCategories("Contacts")
            select (Element)new RootElement(x.Title) {
                new Section(x.Title){
                    (Element)new StyledStringElement("Contact Number",x.Phone) {
                        BackgroundColor=UIColor.FromRGB(71,165,209),
                        TextColor=UIColor.White,
                        DetailColor=UIColor.White,
                    },
                }
            },
        },
    };
ctacke
  • 66,480
  • 18
  • 94
  • 155
Christopher Orchard
  • 1,267
  • 2
  • 12
  • 15

3 Answers3

3

You could use something like:

var root = new RootElement ("Student Guide") {
    new Section("Contacts"){
        from x in AppDelegate.getControl.splitCategories("Contacts")
        let hasPhone = x.Phone == null
        select hasPhone 
         ? (Element)new RootElement(x.Title) {
             new Section(x.Title){
                 (Element)new StyledStringElement("Contact Number",x.Phone) {
                     BackgroundColor=UIColor.FromRGB(71,165,209),
                     TextColor=UIColor.White,
                     DetailColor=UIColor.White,
                 },
             }
         }
         : (Element)new RootElement(x.Title)
    },
};

Or you could break your Linq out to use a method - then it would be less copy and paste of code - e.g.

var root = new RootElement ("Student Guide") {
    new Section("Contacts"){
        from x in AppDelegate.getControl.splitCategories("Contacts")
        select Generate(x)
    },
};

with

private Element Generate(Thing x)
{
    var root = new RootElement(x.Title);
    var section  = new Section(x.Title);
    root.Add(section);

    if (x.Phone != null)
        section.Add(new StyledStringElement("Contact Number",x.Phone) {
                     BackgroundColor=UIColor.FromRGB(71,165,209),
                     TextColor=UIColor.White,
                     DetailColor=UIColor.White,
                 });

    return root;
}
Stuart
  • 66,722
  • 7
  • 114
  • 165
1

Maybe I'm missing something here, but AFAIK, you're just missing a where clause, no?

var root = new RootElement ("Student Guide") {
    new Section("Contacts"){
        from x in AppDelegate.getControl.splitCategories("Contacts")
        where !string.IsNullOrEmpty(x.Phone) 
        select (Element)new RootElement(x.Title) {
            new Section(x.Title){
                (Element)new StyledStringElement("Contact Number",x.Phone) {
                    BackgroundColor=UIColor.FromRGB(71,165,209),
                    TextColor=UIColor.White,
                    DetailColor=UIColor.White,
                },
            }
        },
    },
};
code4life
  • 15,655
  • 7
  • 50
  • 82
0

Potentially use a conditional operator?

string.IsNullOrEmpty(x.Phone) ? "Return Something if it is empty" : x.Phone;

http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx

LukeHennerley
  • 6,344
  • 1
  • 32
  • 50