[CLOSED] LinqDataSource combining columns/aliasing

  1. #1

    [CLOSED] LinqDataSource combining columns/aliasing

    I would like define a column value based on two other columns. I could perform the following option:

    <asp:LinqDataSource ID="LDS" runat="server" ContextTypeName="MyContext"
          TableName="Categories"
          Select="new #RequestCatId, RequestCatAbbr, RequestCatAbbr + " (" + IsActive + ")" as RequestCatAbbrNew, IsActive#
          OrderBy="IsActive desc, RequestCatAbbr" />
    This would provide results like:

    • Baseline (true)
    • Research (true)
    • Archived Documents (false)


    What I really want is:?

    • Baseline
    • Research
    • Archived Documents (Inactive)


    In TSQL the query would look like:
    select RequestCatId, RequestCatAbbr, 
           RequestCatAbbrNew = CASE IsActive when 0 then RequestCatAbbr + ' (Inactive)' when 1 then RequestCatAbbr END,
           IsActive
    from Categories
    order by IsActive desc, RequestCatAbbr
    I could also perform a Union in SQL:
    select RequestCatId, RequestCatAbbr, RequestCatAbbr as RequestCatAbbrNew, IsActive
    from Categories
    where IsActive = 1
    UNION
    select RequestCatId, RequestCatAbbr, RequestCatAbbr + ' (Inactive)' as RequestCatAbbrNew, IsActive
    from Categories
    where IsActive = 0
    order by IsActive desc, RequestCatAbbr
    The question above eventually gets me to the following design. I have a GridPanel that shows the RequestCatAbbr dataIndex. I have added a ListFilter on the dataIndex RequestCatAbbr. I would like the ListFilter labels (LabelField) to be readable and thus use the RequestCatAbbrNew while the IDField will be the raw data RequestCatAbbr.

    I have the ListFilter set up correctly. I just can't build the string that I want to use.

    Any thoughts?
    Last edited by Daniil; Nov 14, 2014 at 7:45 PM. Reason: [CLOSED]
  2. #2
    Hi Chris,

    It looks I don't understand the requirement clearly. How does the serialized data look finally? I mean a JSON object that goes to the Store.

    Ideally, could you provide a test case, please? Without the dependance on LinqDataSource, just with some test data that mimics the real data.
  3. #3
    I guess I was trying to make this harder then it needed to be. I was initially hoping to populate the CategoryStore from the clientside via the LinqDataSource. But I guess I will just do it during PageLoad on the server side and build the data myself after retrieving the rows from the database.

    Here is what I want to see. The ListFilter LabelField is built using the Cat and IsActive attributes while the IdField is the Cat attribute. One wrinkle is that if the IsActive is true the LabelField is just the Cat.

    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html>
    <html>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.GridStore.DataSource = new object[]
                {
                    new object[] { 1, "Baseline", "Training Guide"},
                    new object[] { 2, "Reference", "How to ..."},
                    new object[] { 3, "Archive", "Training Guide (1983)"},
                };
    
                this.GridStore.DataBind();
    
                List<object> data = new List<object> 
                { 
                    new { Cat = "Baseline",  IsActive = true,   CatCombined = "Baseline"           },
                    new { Cat = "Reference", IsActive = true,   CatCombined = "Reference"          },
                    new { Cat = "Archive",   IsActive = false,  CatCombined = "Archive (Inactive)" }
                };
    
                this.CategoryStore.DataSource = data;
                this.CategoryStore.DataBind();
            }
        }
    </script>
    <head runat="server">
        <title>ListFiler Example</title>
    </head>
    <body>
        <ext:ResourceManager runat="server" />
        <ext:GridPanel runat="server">
            <Bin>
                <ext:Store ID="CategoryStore" runat="server">
                    <Model>
                        <ext:Model runat="server" IDProperty="Cat">
                            <Fields>
                                <ext:ModelField Name="id" Mapping="Cat" Type="String" />
                                <ext:ModelField Name="IsActive" Type="Boolean" />
                                <ext:ModelField Name="CatCombined" Type="String" />
                            </Fields>
                        </ext:Model>
                    </Model>
                </ext:Store>
            </Bin>
            <Store>
                <ext:Store ID="GridStore" runat="server">
                    <Model>
                        <ext:Model runat="server">
                            <Fields>
                                <ext:ModelField Name="Id" />
                                <ext:ModelField Name="Category" Type="String" />
                                <ext:ModelField Name="Title" Type="String" />
                            </Fields>
                        </ext:Model>
                    </Model>
                </ext:Store>
            </Store>
            <ColumnModel runat="server">
                <Columns>
                    <ext:Column runat="server" Text="Id" DataIndex="Id" Width="50" />
                    <ext:Column runat="server" Text="Category" DataIndex="Category" Width="120" />
                    <ext:Column runat="server" Text="Title" DataIndex="Title" Flex="1" />
                </Columns>
            </ColumnModel>
            <SelectionModel>
                <ext:RowSelectionModel runat="server" Mode="Single" />
            </SelectionModel>
            <Features>
                <ext:GridFilters ID="GridFilters" runat="server" Local="true">
                    <Filters>
                        <ext:ListFilter DataIndex="Category" StoreID="CategoryStore" LabelField="CatCombined" IDField="id" />
                    </Filters>
                </ext:GridFilters>
            </Features>
        </ext:GridPanel>
    </body>
    </html>
    If you don't have any other thoughts, you can close the thread.
  4. #4
    Please close the thread. Before posting I should go home, relax and think about what I really want to do. Not sure why my brain wanted to do everything on the markup side.

    So in my Page_Load I populate the CategoryStore from the database by the following code:

       CRTSObjsDataContext db = new CRTSObjsDataContext();
       List<CrtsRequestCategory> categories = db.CrtsRequestCategories
          .OrderByDescending(p => p.IsActive)
          .ThenBy(p => p.RequestCatAbbr)
          .ToList();
    
       List<object> data = new List<object>();
    
       foreach (CrtsRequestCategory category in categories)
       {
           object storeData = new
           {
               Cat = category.RequestCatAbbr,
               IsActive = category.IsActive,
               CatCombined = category.IsActive ?  category.RequestCatAbbr : category.RequestCatAbbr + " (Inactive)"        
           }
           data.Add(storeData);
       }
    
       CategoryStore.DataSource = data;
       CategoryStore.DataBind();

Similar Threads

  1. CRUD & Store & LinqDataSource
    By cwolcott in forum 1.x Help
    Replies: 5
    Last Post: Jan 13, 2012, 10:13 AM
  2. Edit master detail with LinqDataSource
    By andrefreitasjr in forum 1.x Help
    Replies: 1
    Last Post: Dec 01, 2011, 8:16 PM
  3. PropertyGrid tutorial with LinqDataSource
    By htaquee in forum 1.x Help
    Replies: 0
    Last Post: Aug 10, 2011, 3:06 PM
  4. [CLOSED] LinqDataSource and remote paging
    By John_Writers in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Apr 28, 2011, 11:22 AM
  5. Grid Panel - LinqDataSource Issue
    By kwerbeach in forum 1.x Help
    Replies: 0
    Last Post: Aug 27, 2010, 9:08 PM

Posting Permissions