linq

Using Linq, order objects by some property and select the first 2 objects

Using a linq query/method chainging I wish to select just the first 2 Point objects in a List ordered by Point.X. How can I? ...

How can I add this stuff up that is coming from a database?

Hi Say I have this in my database courseName : English101 Mark : 80 OutOff : 100 Weight : 50 CourseName : English101 Mark : 30 OutOff : 50 Weight : 10 CourseName: Chem101 Mark : 100 OutOff: 100 Weight : 100 Now I want to find the over all grade for both of these courses First english101. (80 /100) * 50 = 40(achieved weight) (30 ...

LINQ Convert from IGrouping to Lookup

I have two variables of type ILookup. I wanted to use Union or Concat to combine their values and assign the result to a third variable of the same type. Both Union and Concat return IGrouping. It must be dead simple to convert IGrouping to the ILookup but I just can't do it!!! :-( IGrouping exposes just the Key so I am struggling with...

setting UniqueIdentifier from Guid in LINQ, Null Reference exception

I am trying to set a unique identifier in my customer table from the unique identifer from my membership table (asp.net membership). However I am getting a null reference exception when doing so. This is the code. Customer customer = db.Customers.SingleOrDefault(c => c.Id == Convert.ToInt32(formValues["CustomerId"])); Guid userId = ...

Filtering a List with another List Inclusive.

i have a list of filter names: FILTERBYNAMES my query result items each contain a name list: NAMES I want to filter the result an take all items whose name list contains at least one name in the FILTERNAMELIST: results= result.where(r=>r.NAMES.CONTAINS(...?)...? ...

LINQ - Distinct() returning a different value if extension method used

I have a LINQ query which is attempting to get all of the distinct months of all of the dates in a table. I had this working using the Distinct() extension method. I then made it more readable by using an extension method to extract the month. And then it stopped returning Distinct results. Can anyone help me work out what happened her...

Read Excel using LINQ

I want to read excel 2003( cannot change as its coming from third party) and group data in List or Dictionary (I don't which one is good) for example below (Excel formatting ) Books Data [first row and first column in excel] second row( no records) Code,Name,IBN [third row (second column, third column] Aust [fourth row, first colum...

Select DataTable row, filtered by GUID

I have a non-primary-key column with GUID's (with DataType System.Guid) and it appears you cant use DataTable.Select on that column. (Only DataRowCollection.Find but it requires GUID to be Primary Key column, which is not my case) Anyway, I need to get that row WHERE UniqueId = *GUID* Maybe there are some LINQ tricks that can do the jo...

Creating XML from a LINQ result

If I've used LINQ to retrieve results from the database, is there a way that I can automagically get this to generate XML? For example: DataClasses1DataContext db = new DataClasses1DataContext(C_CONN_STRING); var q = from stock in db.GetTable<Stock>() where stock.Group == 1 select stock; foreach(Stock s in q) { //...

Has anyone had any success with Subsonic3, linq, MySQL and Stored Procedures

Hi, I have been trying to get MySQL stored procedures running with the linq templates in Subsonic3. I added some functions to the MySQL.ttinclude file that seems to have generated the stored procedure reference classes. However when I run the code and call the stored procedures I seem to always get NULL results: public DataSet SPTot...

Building custom shaped view model for asp mvc framework

Hi everyone, I am building my first few projects with ASP.NET MVC, and I have a list of items that come from 2 tables. Using the nerd dinner example, this is my problem. Lets say I want to have all the dinners, with all the rsvp information in a list on one page. So it would go dinnername1 rsvp1 rsvp2 dinnername2 rsvp1 rsvp2 rsvp...

LINQ: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<int>' to 'int'

if i do this. int dreamX[]; private void Form1_Load(object sender, EventArgs e) { sumX(); } private void sumX() { for (var i = 0; i < 8; i++) { dreamX[i] = from Control control in Controls where co...

Update XML with C# using Linq

//MY XML FILE STRUCTURE <items> <item> <itemID>1</itemID> <isGadget>True</isGadget> <name>Star Wars Figures</name> <text1>LukeSkywalker</text1> </item> </items> //TO READ DATA FROM XML BY ITEMID XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("data.xml")); var it...

Linq Combine Left Join Data

Say I have the following database: Users ------- UserId (PK) UserName Roles ----- RoleId (PK) RoleName UserRoles --------- UserId (PK) RoleId (PK) Users 1-M UserRoles M-1 Roles Using LinqToSQL, I want to return the following set: [User1], [Role1, Role2, Role3] [User2], [Role2, Role3] [User3], [] Etc... What is the most efficien...

Dynamic Linq Sort Nested Table

Follow up question to this: http://stackoverflow.com/questions/1488414/linq-combine-left-join-data Say I have the following db tables: Users ------- UserId (PK) UserName Roles ----- RoleId (PK) RoleName UserRoles --------- UserId (PK) RoleId (PK) Users 1-M UserRoles M-1 Roles Using LinqToSQL, I can return the following set (thanks...

C# Linq query Descendants method using variables

I am trying to use the C# Linq query Descendants method using variables. Here is part of the XML <CourseList> <course id="EM220" name="Fundamentals of Mechanics"/> <course id="EM305" name="Engineering Tools Seminar"/> <course id="EM320" name="Dynamics"/> </CourseList> Here is the query: static void GetAllCourseIds(XEleme...

LINQ to DataSet case insensitive group by

I have a data table and I want to perform a case insensitive group by over a column of data table (say Column1 of type string). I observed that normally LINQ to DataSet perform case sensitive comparison. For example if Column1 is having two values say Test and test, after applying group by it returns two seperate rows with values Test an...

LINQ to XML query in c#

How can query below document so it would give me value of "id" (the one in the root after status) ? this gives an error (Object reference not set to an instance of an object.) using (WebResponse response = request.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream()...

delete row's selected in Gridview in Asp.net

This Code has a problem , that I don't know what is that . this code must Delete row of gridview when selected. I set AutoGenerateSelectButton = true then write this Code that don't work and have Exception from foreach. protected void IDeletebtn_Click(object sender, EventArgs e) { foreach (GridViewRow Items in InboxGrv.SelectedRow)...

Linq Sort Nested Table

Follow up question to this: http://stackoverflow.com/questions/1488414/linq-combine-left-join-data Say I have the following db tables: Users ------- UserId (PK) UserName Roles ----- RoleId (PK) RoleName UserRoles --------- UserId (PK) RoleId (PK) Users 1-M UserRoles M-1 Roles Using LinqToSQL, I can return the following set (thanks...