C# - Linq: Select First Items From The Child List?

Jun 24, 2010

I have the following one-to-many relation between two objects.

Parent
--> IList<Child>

Now, I have a List of Parent objects and I want the First Child of each parent in the list.

What is the best way to do this using Linq?

View 3 Replies


Similar Messages:

Select Values From A List, Where The Items Contain In A Different List Aswell?

Feb 5, 2010

Is it possible to select values from a List, where the items contain in a different list aswell?for example;

[Code]....

What I need are all the items from the first list "Items", where the string contains something from the second list "List".So I need something which returns the first 3 items (which contain the term "Item") from the first list.

View 3 Replies

Web Forms :: Select CheckBoxList Items Based On LINQ Query Results

Apr 29, 2010

[Code]....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace find.Admin
{
public partial
class WebForm1 : System.Web.UI.Page
{
private
class servicetimeofday
{
public int servicetimeofdayid {
get; set; }
public int serviceid {
get; set; }
public int timeofdayid {
get; set; }
}
protected void Page_Load(object sender,
EventArgs e
{
List<servicetimeofday> servicetimesofday =
new
List<servicetimeofday>
{
new servicetimeofday() { servicetimeofdayid = 1, serviceid = 1, timeofdayid = 1 },
new servicetimeofday() { servicetimeofdayid = 2, serviceid = 1, timeofdayid = 2 },
new servicetimeofday() { servicetimeofdayid = 3, serviceid = 2, timeofdayid = 1 },
new servicetimeofday() { servicetimeofdayid = 4, serviceid = 2, timeofdayid = 3 }
};
GridView2.DataSource = servicetimesofday;
GridView2.DataBind();
var itemstocheck = from
servicetimeofday in servicetimesofday
where servicetimeofday.serviceid == 2
select ne
servicetimeofday.timeofdayid
};
GridView3.DataSource = itemstocheck;
GridView3.DataBind();
foreach (var timeofdayid
in itemstocheck)
{
foreach (ListItem i
in CheckBoxList2.Items)
{
if (i.Value == timeofdayid.ToString())
{ i.Selected =
true; }
}
}
}
}
}

View 10 Replies

DataSource Controls :: Insert A "Select User" Row In Linq For A Dropdown List?

Sep 7, 2010

I'm new to Linq. I have searched and searched the web for a soluion, and can't find anything. I have a Linq query and I want to insert a row ("Select User") to the top before I pass it to the drop down list. I've been trying to use the Union but to now avail (it keeps telling me that my object doesn't support the Union method). My code, prior to attempting to inser a row, is very simple.

public SelectList DropDown_Users()
{
var context = new VivarianDataContext();
var query = from t in context.AspnetUsers
select new { t.UserId, t.LastName };
list = new SelectList(query.AsEnumerable(), "UserId", "LastName");
return list;
}

Now I try to insert a row and I found this on the internet and it seems to say that his solution will work. But it is filled with syntax errors. [URL]

I tried to implement it using the following code, but it doesn't compile.

public SelectList DropDown_Users()
{
SelectList list;
//get the original data
var context = new SQL2005633131VivarianDataContext();
var query = from t in context.AspnetUsers
select new { t.UserId, t.LastName };
//create a dummy table with an empty row
var AllUsers = new List<AspnetUsers>();
var BlankUser = new AspnetUsers()
{UserId=System.Guid.Empty, LastName="Select One"};
AllUsers.Add(BlankUser);
//use Union to join the data - ERRORS HERE
var newTable = AllUsers.Union(query);
list = new SelectList(newTable.AsEnumerable(), "UserId", "LastName");
return list;
}

View 7 Replies

Ordering Sub-items Within Ordered Items In A Linq To Entities Query?

Aug 25, 2010

I have a few tables, Listings, ListingImages and a few others related to Listings. ListingImages is related to Listings so that you can have many ListingImages per Listing.

When I query this table I do;

[code].....

Now this is fine. However, I now want to sort the ListingImages independently within each Listing (by an ImageOrder column I have in that table).

How can I do this and pass all my Includes(...). Would it be bad form to sort the ListingImages within the View as this solution seems to work?

View 1 Replies

Linq Query - How To Select Inner Query Into List

Oct 17, 2010

I am writing a linq query to select a blogpost,

[code]....

The blogpost i am testing got 3 tags attached to it. The table structure is:

(table)BlogPost -> (table)BlogPostTags <- (table)Tags

So the BlogPostTags table only contains 2 fields, BlogPostID and TagID.

When i run the query above i get 3 results back. Same blogpost 3 times but with 1 tag in each. It should return 1 post with 3 tags. The problem lies in the Tags query above.

View 1 Replies

ADO.NET :: Difference Between Select And Select New In Linq Query

Aug 17, 2010

What is difference between select { } and select new {} In Linq Query

View 3 Replies

Forms Data Controls :: Bind Checkbox List To Selected Items List?

May 27, 2010

I have a situation where I want to show the selected records out of total records for a product of an employee

1) There is a checkbox list bind to <List> of objects from object datasource (For total items in list)

2) Now I want to check the selected items for a particular record in this list

3) For this purpose I have another list of <List> selected items returned by data access layer (For selected items for that employee )

4) How do I bind the selected objects with the total items list ?

5) In spaghetti coding model it was all too easy just by binding the checkbox list with a sql data source and running a for each on form load

View 5 Replies

Web Forms :: Adding List Items To Drop Down Control From Generic List?

Feb 6, 2010

I have the following Students class:

[Code]....

I need the 1 since it's a foreign key in another table. For the life of me, I can't get this to work like this.

View 7 Replies

Web Forms :: Tab Index For List Items In Radio Button List Control

Oct 17, 2012

Is it possible to set tab index value for list items of ASP.Net RadioButtonList control.

View 1 Replies

Templates - Populate List Items In Dropdown List From Placeholder?

Feb 24, 2010

I'm designing my own custom control that contains a .NET dropdownlist. What I'm wondering is if it is possible to populate my dropdownlist with listitems placed in a placeholder? For example:

<asp:DropDownList ID="ddlFilter" runat="server" >
<asp:PlaceHolder ID="ListItemPlaceholder" runat="server"/>
</asp:DropDownList>

This doesn't work because the DropDownList control only allows ListItems as child controls. But, I want to do something similar to this so when the user includes my control on a page, they can do something like this:

<mytag:MyControl Mode="DropDown" runat="server">
<ListItemTemplate>
<asp:ListItem Text="C" Value="c"></asp:ListItem>
<asp:ListItem Text="E" Value="e"></asp:ListItem>
<asp:ListItem Text="B" Value="b"></asp:ListItem>
</ListItemTemplate>
</myTag:MyControl>

I know I can do this by dynamically adding the ListItems in the page code behind, but I'd like to avoid that if possible.

View 1 Replies

Drop Down List Has A SelectedValue Which Is Invalid Because It Does Not Exist In The List Of Items

Mar 25, 2011

I am getting this error yet I know it to be untrue.This is the code:

ddlPartnerOrganisation.DataSource = agency.AgencyGetListOfEYDN();
string temp = ddlPartnerOrganisation.SelectedValue.ToString();
ddlPartnerOrganisation.DataValueField = "AgencyID"
;
ddlPartnerOrganisation.DataTextField = "AgencyName"
;
ddlPartnerOrganisation.DataBind();

View 4 Replies

ASP Menu ...Alignment For Child Items In Firefox Not Work?

Feb 24, 2011

I'm using ASP Menu .... It Works fine in IE ...but The Problem in Fire fox ... i put the asp menu inside Table Cell and I give Alignment Property for that cell to be right .....the Alignment for Childs in IE is Right Correct but in FireFox is Left :-( so how can I change the alignment for Childs to be Right and to work in Firefox

The ASP Menu Code in ASPX is :

[Code]....

View 2 Replies

Web Forms :: Child Parent Class In Linq

Apr 27, 2016

I want to write the Customer belongs their product

Customer 1  and their details

then products details Purchased by Customer 1

Customer 2  and their details

then products details Purchased by Customer 2

public class Customer {
public int CustID { get; set; }
public string CustName { get; set; }
public string CustEmail { get; set; }
public string CustContact { get; set; }
public string CustAdd { get; set; }

[Code] .....

View 1 Replies

Execute LINQ Query Using Properties Of Child Entity Set

Nov 25, 2010

I am having a meltdown over something that seems so simple and yet isn't working. Here's my scenario.

I have an object structure of tEvents, which contains properties of an event like a run of concerts. tEvents in turn contains an entity set of tEventOptions, which include properties like EventDate, Cancelled etc. I would like to query a list of tEvents using properties of the tEventOptions, for example filtering by date.

My pageis using a calendar object to show event dates. So I am trying to find if any tEvents in the List coming back from the database match the date of the day element being rendered in my Calendar control like so (in this snip 'data' is List and results from a db query):

[code]....

Except r, my query result var, is always true no matter what data it receives. I have tried the sub query using other properties of tEventOption and get the same 'true' result each time. I know that the data does not reflect this result so I clearly have a problem with the structure of my query, but for the life of me I cannot find a way to resolve it.

View 1 Replies

C# - How To Write A Single LINQ To XML Query To Iterate Through All The Child Elements

Aug 14, 2010

I am developing asp.net mobile application. I am using XML as a database. I am querying on the XML to access the required elements & attributes by using LINQ to XML in .net. I have the follwing part in my XML file.

[code]....

The similar logic I need for the above defined XML part. In the above XML I want to write the LINQ to XML query which can access the NAME attribute of the 'VALVE' node (<VALVE NAME="PWV">), then it should access the text between 'DISPLAY-NODE' (<DISPLAY-NAME> Production Master Valve </DISPLAY-NAME>), & then it should access the all the attributes of the 'COMMAND' node dynamically ( <COMMANDS USE-TEMPLATE="TRUE" TEAMPLATE-NAME="ValveCommands.xml"></COMMANDS>). All these I want dynamically without explicitly specifying the name of the child node as well as name of their attributes ( similar to the way I written the above query ) Can we write such a code by using LINQ to XML ? It will be better if we can write code for above issue in a single logic ( similar to the way I written the above query ).

View 1 Replies

SQL Server :: Select All Childs Node For A Parent With No Further Child In A Table?

Jan 11, 2011

i have a table as child_id,parent_id,child_name,level i want to select all child(leaf nodes) with no further child for a particular parent in table.

[Code]....

View 1 Replies

MVC :: Saving A Complex Child Object After Post (LINQ To SQL Model Binding With MVC)

Jan 6, 2011

I am using ASP.Net MVC2 and LINQ to SQL. I am using DataAnnotations and model binding in conjunction with a Form/View and its working beautifully well. Loving that.. My view is an "Add new Employee" form that's adding a new "Employee" complex object. Every Employee also has a child complex object called an "Address".
I am using the bound Html helpers in the view, such as Html.TextBoxFor(model => model.NewEmployee.FirstName) for example. Working beautifully well, validating beautifully well both client and server and coming into the controller fully populated and ready to be saved! Exciting.. ModelState.IsValid returns true and I go to save the new Employee.

The Employee is created fine but the system also creates a blank/new Address record in my Addresses table even when the Address fields are blank! How can I prevent any Address from being created, in situations where I ONLY want to create a new Employee with no Address. An example is when the user supplies an existing Address, then I wish to simply set the new Employee's AddressID to that of an existing physical address (I hope that makes sense). But it could apply to any situation where you need to create a new complex object, but you don't wish for LINQ to create any of its child complex objects. I have tried setting NewEmployee.Address = null after it is received in the controller, but even after that, a blank/new Address is still created. Even if I try NewEmployee.Address = SomeExistingAddress, it does correctly link the Employee to the existing Address, but it STILL goes off and creates another redundant new/blank address record! Which is really weird..

View 10 Replies

MVC + LINQ To SQL Model Binding - Saving A Complex Child Object After Post

Jan 5, 2011

I have a "Create New Employee" ASP.Net MVC form. My complex object is an Employee and an Employee has an Address, which is another Complex object. On my View/Form I collect all required values for both the Employee and the Address. I'm using the bound HTML helpers such as: Html.TextBoxFor(model => model.EmployeeAddress.StreetName)

AND

Html.TextBoxFor(model => model.NewEmployee.FirstName)

This is all working beautifully well. Model binding is working like a dream, both server side and client side validation using DataAnnotations is working beautifully well and I am nicely receiving my populated complex objects as expected in the Controller.. Now I'm trying to save.. The Employee should always be newly created, because its an "Add New Employee" form. But sometimes the Address is an existing Address and I don't want to insert another one. Rather, I just want to link the Employee to the existing AddressID of the one that already exists in the database.

So I wrote a nifty GetExistingOrCreateNewAddress(Address PostedAddress) method which works great so I end up with the correct Address to use and link to the about to be saved Employee object. This is all happening in the same DataContext, so no problems there..

BUT even when I link the about to be saved Employee object to an existing Address, on save a new/empty Address row is created in my Addresses table. Even though the newly created Employee does link correctly to the existing Address I told it to! Why is it so??? And how can I save the new Employee without LINQ automatically creating a blank Address for me. Because I'm explicitly specifying an existing Address it should be linked to instead!

[HttpPost]
public ActionResult CreateEmployee(EmployeeDetailsViewModel NewEmployeeDetails)
{
if (ModelState.IsValid)
{
EmployeeRepository ER = new EmployeeeRepository();
// Fetch or Create the appropriate Address object for what has been entered
Address ActualAddress = ER.GetExistingOrCreateNewAddress(NewEmployeeDetails.EnteredAddress);
// Link this Address to the "about to be saved" Employee
NewEmployeeDetails.Employee.Address = ActualAddress;
// Lock it in..
ER.SaveNewEmployee(NewEmployeeDetails.Employee);

View 2 Replies

Find All Child Controls Of Specific Type Using Enumerable.OfType<T>() Or LINQ?

Feb 5, 2010

Existed MyControl1.Controls.OfType<RadioButton>() searches only thru initial collection and do not enters to children.

Is it possible to find all child controls of specific type using Enumerable.OfType<T>() or LINQ without writing own recursive method? Like this.

View 1 Replies

Select A Random List From A List Of Data?

Jan 12, 2011

I have customer data in MSSQL database. More than 1million records i have. I want to select a random 2000 records

from the one million record and do some manipulation. After manipulating the data i want to store that data to some other database.

How to achive it in c# and MSSQL2008?

View 3 Replies

ADO.NET :: Delete The Items In Db Using Linq To Sql?

Jan 14, 2011

I want to delete the items in db using linq to sql below is my code

[Code]....

here i am very much confused about this var delt= from x what is x here? and dc2.emps.deleteonsubmit (here what is emps?)i am getting errors like emp id doesnt exist in the current context.

View 4 Replies

C# - Select All Items In The Listbox?

Jun 29, 2010

if checkbox is clicked i need to select all items in the listbox in asp.net...

how to show the items all selected in listbox once the 'selectall' checkbox is clicked

View 2 Replies

Data Controls :: Bind DataGridView For Particular Childnode (ID) When Select / Click Any Particular Child Node

Mar 31, 2014

There are multiple child nodes coming from Database in Treeview. Also, there is a DataGridview.

I want that when I select/click any of the child node from TreeView, it should show the related data(of selected child node) in DataGridview from Database on "Page Load".

My current code is below, which only binds all the data in DataGridview and Treeview from Database:

public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
BindGrid();
FillchildNode();

[Code] .....

View 1 Replies

Web Forms :: How To Select All Items In Listbox

Nov 16, 2010

I start building asp.net web site for my office. I have a listbox with province ID and name. How do i select all item on page_load?

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved