Selecting individual records in a declarative linq data source?

How to select individual records when using a declarative data source?

<asp:LinqDataSource ID="dsColors" runat="server" ContextTypeName="Context" OrderBy="Id, Name" Select='new (Id, String.Concat(Id + ": " + Name) as ColorName)' TableName="Colors"> </asp:LinqDataSource> 
+4
source share
3 answers

Hack it using the By group.

 <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> 
+4
source

Better do it by choosing

 <asp:LinqDataSource ID="dsColors" runat="server" ContextTypeName="Context" OnSelecting="dsColors_Selecting" TableName="Colors"> </asp:LinqDataSource> 

then in code <

 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 like that should work.

+1
source

Using group by , an object called key .

Group by one value:

 GroupBy="Name" Select="new (key as Name)" 

Group by several values:

 GroupBy="new(ID, Name)" Select="new (key.ID, key.Name)" 

To access other properties that are not part of the group, use aggregated queries :

 GroupBy="new(ID, Name)" Select="new (key.ID, key.Name, min(OtherValue) as OtherValue)" 
0
source

Source: https://habr.com/ru/post/1313012/


All Articles