如何在使用声明性数据源时选择不同的记录?
<asp:LinqDataSource ID="dsColors" runat="server"
ContextTypeName="Context"
OrderBy="Id, Name"
Select='new (Id, String.Concat(Id + ": " + Name) as ColorName)'
TableName="Colors">
</asp:LinqDataSource>
答案 0 :(得分:4)
使用分组依据来破解它。
<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>
答案 1 :(得分:1)
选择
时最好这样做<asp:LinqDataSource ID="dsColors" runat="server"
ContextTypeName="Context"
OnSelecting="dsColors_Selecting"
TableName="Colors">
</asp:LinqDataSource>
然后在
背后的代码中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;
}
在某种程度上应该有效。
答案 2 :(得分:0)
使用group by创建一个名为key
的对象。
按单个值分组:
GroupBy="Name"
Select="new (key as Name)"
按多个值分组:
GroupBy="new(ID, Name)"
Select="new (key.ID, key.Name)"
使用aggregate queries访问不在群组中的其他属性:
GroupBy="new(ID, Name)"
Select="new (key.ID, key.Name, min(OtherValue) as OtherValue)"