1

I have a table Menu and the data is like the attached screen shot Database table

and I want output like below image,

http://s16.postimg.org/6evrtzkwl/output.png

Means all the null values or integer ==0 should be hidden.

Right now I am showing these 5 columns only as you can see in 2nd screen shot..

I have something like this..

List<Menu> lstMenus = obj.GetMenus(10);

My code is

var menus = new List< dynamic >();

foreach (Menu menuBE in lstMenus)
    {

        dynamic menu = new
        {

            menuBE.MenuID,
            menuBE.ParentMenuID,
            menuBE.LinkText,
            menuBE.ScreenName,
            menuBE.Parameters,
            menuBE.URL
            // if(menuBE.Parameters.Length>0 ){ Parameters = menuBE.Parameters,}
        };
        menus.Add(menu);
    }

and I want to put some condition like the last commented line in foreach loop. If menuBE.Parameters.Length>0 then this Parameters column should add be in dynamic menu else not. Any idea how it can be done?

  • Have you tried left joining on the same table to remove null entries – liquidsnake786 Nov 29 '13 at 13:28
  • @liquidsnake786 : can you please suggest how? data coming in my lstMenus is correct. So I have to make change in while add to **var menus** – vishwarajmalik Dec 02 '13 at 05:26
  • What exactly does showing the Params do? Is it something that can be controller by a visible property? Maybe show the menuBE and menu classes to give a better understanding – Lotok Dec 02 '13 at 08:53
  • @James : menus will used to return the json response. So I only want those columns in json response which have values. and MenuBE is the replica of this http://s2.postimg.org/x67jdi0op/table.png see the image for better understanding.. – vishwarajmalik Dec 02 '13 at 09:56
  • If the parameter is for the QueryString, will there likely be more than 1 parameter in the QueryString at any one time? I am thinking a coalesce is probably all you need. – Lotok Dec 02 '13 at 10:04
  • @James : I am using this code in WEBAPI and returning json response from the API. So I only want those fields in response which dont have NULL or Blank values. I have nothing to do with the QueryString. The answer from **André Figueiredo** solves my problem but only for tables, In which I have to check 1 or 2 columns. In some tables I have to 15-20 columns, in that case It will not help.. – vishwarajmalik Dec 02 '13 at 12:37

2 Answers2

1

What I understood, in the opposite of James & techloverr, you want to keep records with parameter null:

foreach (Menu menuBE in lstMenus){
    if (menuBE.Parameters.Length > 0){
       dynamic menu = new{
           menuBE.MenuID,
           menuBE.ParentMenuID,
           menuBE.LinkText,
           menuBE.ScreenName,
           menuBE.Parameters,
           menuBE.URL
       };
    }
    else {
       dynamic menu = new{
           menuBE.MenuID,
           menuBE.ParentMenuID,
           menuBE.LinkText,
           menuBE.ScreenName,
           menuBE.URL
       };
    }
    menus.Add(menu);
}


** UPDATE **

As I understand in the whole question, you do not want a property Parameters when source data have Parameters.Lenght == 0, and that's why you are using dynamic type.

It's different from "that's OK have a property 'Parameters = null'". If that's the approach, you don't need to use dynamic type. Just add items as list-item of a declarated strong-type variable or list-item of anonymous type.

With the dynamic type you add properties at declaration time. So, you have to separate the different assigns as the code above. If you want to put if outside to avoid duplicate code, you use ExpandoObject:

var m = new List<System.Dynamic.ExpandoObject>();
foreach (string item in new string[] { "a", "b", "c" }) {
    dynamic menuItem = new System.Dynamic.ExpandoObject();

    menuItem.pos1 = item;
    menuItem.pos2 = (item == "b" ? item : null); // wrong

    if (item == "c") {           // correct
        menuItem.pos3 = "I am at third iteration";
    }

    m.Add(menuItem);
}

See, if you put a breakpoint at m.Add(menuItem); these are the results:
case item == "a":

menuItem.pos1 = "a";
menuItem.pos2 = null; // wrong: pos2 must not exists

case item == "b":

menuItem.pos1 = "b";
menuItem.pos2 = "b";

case item == "c":

menuItem.pos1 = "c";
menuItem.pos2 = null; // wrong: pos2 must not exists
menuItem.pos3 = "I am at third iteration"; // correct: pos3 only exists here.
Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
  • 1
    Yeah, I think I may have misinterpreted looking back on it. Not much I can do to enhance what this answer gives really so just leaving mine as is :) – Lotok Nov 29 '13 at 14:42
  • 1
    @andre this is the lowest quality coding I have seen – techloverr Nov 30 '13 at 03:54
  • yes I need somthing like this but I have to check around 15-20 column so in that case will have to put many if conditions.. can't i add if condition inside this code .. dynamic menu = new{ menuBE.MenuID, menuBE.ParentMenuID, menuBE.LinkText, menuBE.ScreenName, menuBE.Parameters, menuBE.URL }; – vishwarajmalik Nov 30 '13 at 08:51
  • @vishwarajmalik you can use above answer – techloverr Dec 02 '13 at 04:21
  • Yes this will work but I have to check in around 15 columns so will have to put 15 if else if blocks.. and for first if there will be 15 conditions in a single If. I don't want to follow that approach.. – vishwarajmalik Dec 02 '13 at 07:04
  • Sure, you can (and should) refactor this code. You can put it in a helper function or use ExpandoObject: http://stackoverflow.com/questions/4938397/dynamically-adding-properties-to-an-expandoobject – Andre Figueiredo Dec 02 '13 at 13:11
  • @AndréFigueiredo : Yes I think this will solve my problem. Let me check with this code and thn I will get back to you.. Thankyou all for your replies.. :) – vishwarajmalik Dec 03 '13 at 05:30
  • It is working with foreach loop.. Can I use this `ExpandoObject` in a select statement like below.. `countriesBE.Select(countryBE => new { \\ my condition here });` – vishwarajmalik Dec 03 '13 at 10:13
-1

you can use this code snippet

  foreach (Menu menuBE in lstMenus)
    {

        dynamic menu = new
        {

          MenuID = menuBE.MenuID,
          ParentMenuID =  menuBE.ParentMenuID,
          LinkText =  menuBE.LinkText,
          ScreenName = menuBE.ScreenName,

          URL = menuBE.URL,
            Parameters = (menuBE.Parameters.Length>0) ? menuBE.Parameters : null
        };
        menus.Add(menu);
    }
techloverr
  • 2,597
  • 1
  • 16
  • 28