score:2
The is iterating over a collection that contains MilhouseHour objects: the IGrouping. MilhouseHour does indeed contain a reference to aspnet_User, but the thing that you want to refer to, the name (and the id) is a property of the aspnet_User, not the MilhouseHour. Ideally, you'd be able to write markup like
<asp:CheckBoxList ID="shiftsSun" runat="server"
DataSource='<%#Eval("hourUsers")%>'
DataTextField = "aspnet_User.UserName"
DataValueField = "aspnet_User.UserId" />
But that gives an error. ASP.Net is expecting in the DataTextField the name of a property that it will invoke (presumably by reflection) on the object in question, not an expression to evaluate. The object in question is a MilhouseHour, which doesn't have a property called "aspnet_User.UserName".
You have a number of options.
- Do what you did originally, and group x.aspnet_User instead of x in your LINQ expression. Then the thing iterated over by the CheckBoxList will be an IGrouping, which will have properties "UserName" and "UserId" that will work.
Avoid using CheckBoxList and use something that will let you control the code in the inner loop a bit more explicitly, such as repeater:
<asp:TemplateField HeaderText="Sunday"> <ItemTemplate> <asp:Repeater ID="shiftsSun" runat="server" DataSource='<%#Eval("hourUsers")%>'> <ItemTemplate> <asp:CheckBox runat="server" Text='<%# Eval("aspnet_User.UserName") %>' Value='<%# Eval("aspnet_User.UserId") %>' /> <br /> </ItemTemplate> </asp:Repeater> </ItemTemplate>
There are probably many more options, but that's what occurs to me at the moment.
score:1
I don't think you're missing any major concepts. The group query produces a collection of IGrouping, where Whatever is the type of the objects in the MilhouseHours collection. (I can't get a bigger picture for your object model.) You then bind those groupings to a CheckBoxList's data source. You're missing a DataTextField="NameField" directive, but no major concepts. Here's some code that works, that's at least related to your problem, if not identical:
<asp:GridView ID="myGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("hourUsers.Key") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sunday">
<ItemTemplate>
<asp:CheckBoxList ID="shiftsSun" runat="server"
DataSource='<%#Eval("hourUsers")%>'
DataTextField = "Name"
DataValueField = "Id" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And in the pagebehind:
class UserHour
{
public string Name {get;set;}
public int Id { get; set; }
public string Hour { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
var users = new List<UserHour>
{
new UserHour {Name = "Fred", Id = 1, Hour = "0800"},
new UserHour {Name = "Fred", Id = 1, Hour = "0900"},
new UserHour {Name = "Fred", Id = 1, Hour = "1000"},
new UserHour {Name = "Bob", Id = 2, Hour = "0900"},
new UserHour {Name = "Bob", Id = 2, Hour = "1000"},
};
var result = from x in users
group x by x.Hour
into hour select new
{
hourStr = hour.Key, hourUsers = hour
};
myGrid.DataSource = result;
myGrid.DataBind();
}
Source: stackoverflow.com
Related Articles
- Accessing info from FK-related tables with LINQ
- What's the best way to copy and insert data from related tables into a new set of related tables with Linq
- LINQ to SQL: Complicated query with aggregate data for a report from multiple tables for an ordering system
- Joining two tables with LINQ while also returning null records from the second table
- Select data from multiple unrelated tables with LINQ to Entity Framework
- Populate a C# class from a dataset with multiple linked tables using linq lambda
- creating Linq to sqlite dbml from DbLinq source code
- Gettings grouped sums from related tables into linq query columns
- Linq result from many tables into an inherit class with nested classes
- Accessing SQL Server time in code with LINQ
- Can't add a new record with an integer value into database by using linq from code C#
- Showing data from different tables with Linq C# Asp.net
- How to get entities from multiple tables with one LINQ call?
- Retrieving data from related tables via EF6 and Linq
- Return data from related records with Linq to Entities
- Select all fields from collection with related items in another collection using LINQ
- LINQ query from two related lists with specific criteria
- MVC3 Linq updating two tables with data from one model
- How to select data from multiple tables with LINQ (entities)
- LINQ Get Data from 2 tables in 1 join with group by
- Try to get total price with id from 2 tables with Linq
- c# Linq or code to extract groups from a single list of source data
- Linq with sum and group from two tables
- Linq join on multpile columns with a where not equals condition and need to return columns from both tables
- select from multiple tables with linq to sql
- Select from mulitle tables with count in Linq
- Find a certain value from a source collection in a target collection of different type with linq
- Linq left outer join across multiple tables retrieving info from each table
- Entity Framework dynamic linq where from generic source with dynamic where clause
- I want to bring data from multiple tables with entityframework using linq method in dotnetcore web api controller
- How can I convert all DateTime properties to a different TimeZone in a collection?
- Scalar Function SQL By Linq Vb.net
- Loop and update object value in a collection using LINQ
- Get XElement from XML using LINQ
- Join Two Array based on Index and Index Value using Linq Query Expression Syntax
- Issue converting varchar to datetime in bulk insert statement
- LINQ Weirdness with joining tables
- How can I perform OrderBy using LINQ on XML data?
- Make JOIN query match even if row being joined does not exist
- LINQ to SQL -> Cannot access a disposed object. Object name: 'DataContext accessed after Dispose
- MVC3 Razor View Engine
- Newly created item field values
- Getting a sub set of SortedDictionary as a SortedDictionary
- Why is my linq query adding the same item to a list instead of all items?
- Using Linq to Get List of Strings from Json
- How do I GroupBy from two Lists?
- ORA-00923 dot net core devart provider FirstOrDefault() query error
- c# remove elements on a list by comparing with other lists
- Linq with expect: Only primitive types or enumeration types are supported in this context
- Using SQL Wildcards with LINQ