score:2
well you can always do this for the query portion, i'm guessing that's where you are getting tripped up:
var schools = db.schools
.where(x => x.state == "<whatever>")
.select(x => new {
school = x,
bobs = x.where(y => y.middlename == "bob")
}).tolist();
list<schools> schoolsview = new list<schools>();
foreach(var x in schools)
{
schoolsview.add(new schoolsviewmodel(){
//set properties here
schooldid = x.school.id,
schoolname = x.school.name,
students = x.bobs.tolist() //you can project here if needed.
};
}
return schoolsview;
then for your view you can do this any number of ways but if you have the concrete viewmodel with the fixed output it's just nested loops:
<ul>
@foreach(var m in model) //assuming your model is a list of schoolviewmodels
{
<li>@m.schoolname
<ul>
@foreach(var s in m.students)
{
<li>@s.name</li>
}
</ul>
</li>
}
</ul>
you also have cute options for doing things like string.join(",", model.students)
to output the list but this is up to you.
edit: additional clarification
you will also want to change your models a tad, i'm not sure what ef version you are using but in ef4.1+ you can specify your navigation properties to remove the need for an explicit join condition (which is the right approach in the end).
http://msdn.microsoft.com/en-us/library/bb738520.aspx
so your schools model would become:
public class school
{
public int schoolid {get; set;}
public string schoolname {get; set;}
public int stateid {get; set;}
public virtual ilist<student> students {get; set; }
}
and then in your db configuration (if you are using fluent the configuration would be:
modelbuilder.entity<school>()
.hasmany(x => x.students).withrequired();
then you gain the power of doing
db.schools
.where(x => x.name.contains("some value"))
.include(x => x.schools.where(x => x.middlename.contains("somevalue")))
.tolist();
which even makes my prior query even more simple without needing to use ambiguous type definitions.
hopefully this clarification helps a bit further.
score:1
you could try something like this to display the data.
top level view:
@model schoolviewmodel
@using (html.beginform())
{
<fieldset>
@html.editorfor(x => x.bobs)
<input type="submit" value="save" />
</fieldset>
}
you could then use editor templates for display purposes. create them at the following url: ~/views/shared/editortemplates/student.cshtml
@model student
@html.displayfor(x => x.studentid)
@html.displayfor(x => x.schoolid)
@html.displayfor(x => x.firstname)
@html.displayfor(x => x.middlename)
@html.displayfor(x => x.lastname)
you can then put any sort of formatting you'd like on the sublist generated. each student in the bobs ienumerable will render according to the rules you define in the editor template.
as far as actually querying the database goes, i would create some sort of repository along this pattern:
public interface irepository<tentity> where tentity : class
{
list<tentity> fetchall();
iqueryable<tentity> query { get; }
void add(tentity entity);
void delete(tentity entity);
void save();
}
a concrete implementation of which would look like:
public class sqlrepository<t> : irepository<t> where t : class
{
private datacontext db;
public sqlrepository()
{
this.db = new testdatacontext();
}
public void add(t entity)
{
db.gettable(of t)().insertonsubmit(entity)
}
public void delete(t entity)
{
db.gettable(of t)().deleteonsubmit(entity)
}
public system.collections.generic.list<t> fetchall()
{
return query.tolist();
}
public system.linq.iqueryable<t> query {
get { return db.gettable<t>(); }
}
public void save()
{
db.submitchanges()
}
}
Source: stackoverflow.com
Related Query
- How to query a complex result set from EntityFramework and display the result set in MVC.
- How to query a DataSet and set the result as a DataSource for some control? (C# winforms)
- How to execute a linq query for each item in a list , use it in the where clause and return a collection from the result of each query?
- How to iterate over results of a linq-to-sql query result and append the results?
- how to print a result of linq query from controller in the view?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to return values from a LINQ query and display them in a table using C#, ASP.NET MVC and Entity Framework
- How to Iterate over an XML Element result set from a Linq Query
- how to read items from file into a list of items and set the properties to the value in the file?
- Linq - How to take the result for a query in Linq and add it to an Array
- How to join two arrays in a LINQ query and use the result further in the query?
- How to query the average from a certain property in an Entity and return it in a List of List of objects in LINQ?
- Comparing data from 2 tables and display the result on a dropdownlist using LINQ
- NDepend: how to export the result from a query
- How to include counted property in a query and rename the column in the result in c#
- How to query values associated with the foreign key using linq and display in listview
- How to write LINQ query for fetching the specific records and generating new result at the same time?
- How do I use the having clause on an aggregate in a complex group by and join query in LINQ
- How to write SQL and LINQ to get the one record from a result with where condition?
- How can I group by in LINQ and then take first from the group from a sequential set of checks
- How to match value from textbox to dictionary and display it in another textbox on button click with linq query in C#
- How to order a LinQ result set created by a list of ID and order it use existing order of the list
- LINQ query to join two sets and filter on the result set
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- How to assign LINQ Query to a variable and then use it later in the code
- How to set the dropdown display default to "Medium" and not to be "High"?
- Take value from part of a LINQ query and add it to the result
- Looping LINQ query using do while and display the result
- how to combine and sum up the result from two lists based on unique id
- How to choose columns from a query and display in datagrid
More Query from same tag
- Why should I choose Crystal Report or SSRS over plain HTML table?
- Linq Expression Selector
- Comparing String to Integer in LINQ
- Trying to convert PIVOT SQL into LINQ lambda exp
- LINQ Expression - Same value no more than twice
- Conditional SELECT in Lambda expression LINQ
- How do I get all invoices for a customer using the Linq extensions in QuickBooks Online API v3?
- Change a Linq object inside foreach statement?
- How to filter files from directory using LINQ?
- Check if a specific value appears more than once in a table with a LINQ query
- Catch exception inside Linq
- Linq Equivalent to Where Clause with Nested Conditions
- Setting an existing value for dropdown in MVC C# with Linq
- How to "split up" or optimize a Linq to Sql query.
- Dynamic Linq to add LIKE to where clause
- LINQ GroupJoin to IGrouping
- Get count of multiple values in a dataset table
- Parse a string to predicate
- How to join together all the elements in an IEnumerable of IEnumerables?
- Linq query foreach element in array
- How to remove an item in a list that contains specific string
- Operate on a list of numbers using LINQ
- Linq to objects: inner query performance
- Linq to Entities left join return 0 records
- Where clausole in LINQ on Razorpage don't work
- copying double[] values into an entire column in a datatable rapidly
- Find underlying tables of views using Linq to Entity to make Aggregate Dependencies
- Dynamic Linq Library can’t handling duplicate alias column names
- Why is the LINQ "apply-to-all" method named Select?
- how to order a group result with Linq?