How can I select distinct records when using a declarative data source?
<asp:LinqDataSource ID="dsColors" runat="server"
ContextTypeName="Context"
开发者_C百科 OrderBy="Id, Name"
Select='new (Id, String.Concat(Id + ": " + Name) as ColorName)'
TableName="Colors">
</asp:LinqDataSource>
Hack it with a Group By.
<asp:LinqDataSource ID="dsColors" runat="server"
ContextTypeName="Context"
OrderBy="Id, Name"
Select='new (Id, String.Concat(Id + ": " + Name) as ColorName)'
TableName="Colors"
GroupBy='new(Id,Name)'
>
</asp:LinqDataSource>
It's better to do it via on selecting
<asp:LinqDataSource ID="dsColors" runat="server"
ContextTypeName="Context"
OnSelecting="dsColors_Selecting"
TableName="Colors">
</asp:LinqDataSource>
then in the code behind
private Context ctx;
protected void Page_Init(object sender, EventArgs e)
{
ctx = new Context();
}
protected void dsColors_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
var results = (from c in Colors
select c.ID).Distinct();
e.Result = results;
}
something to this extent should work.
Using group by creates an object called key
.
Group by single value:
GroupBy="Name"
Select="new (key as Name)"
Group by multiple values:
GroupBy="new(ID, Name)"
Select="new (key.ID, key.Name)"
To access other properties not in the group by use aggregate queries:
GroupBy="new(ID, Name)"
Select="new (key.ID, key.Name, min(OtherValue) as OtherValue)"
精彩评论