PDA

View Full Version : [CLOSED] LinqDataSource combining columns/aliasing



cwolcott
Nov 13, 2014, 1:18 PM
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?

Daniil
Nov 14, 2014, 4:33 AM
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.

cwolcott
Nov 14, 2014, 1:46 PM
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.

cwolcott
Nov 14, 2014, 4:43 PM
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();