linq

Sort one C# list by another

I have 2 list objects, one is just a list of ints, the other is a list of objects but the objects has an ID property. What i want to do is sort the list of objects by its ID in the same sort order as the list of ints. Ive been playing around for a while now trying to get it working, so far no joy, Here is what i have so far... ...

Has LINQ completely changed the way you code?

Ever since I learned LINQ a couple years ago I have been using it more and more. I no longer think in loops but rather in projections, selections, etc. Even when I use other languages like Perl, I now find myself making extremely heavy use of things like map and grep. I find that pretty much every single method which has more than 3...

Replace {#Text} and {$Text} in a string, in a performant way

NOTE: This is not a simple string replace question. SomethingBlahBlah can be #SomeThingOtherthanThis My case is as follows, I have a large string (<1024B, but >300B), that will have {#String} and {$String}. To be more specific {#SomethingBlahBlah} and {$SomeOtherThingBlahBlah}, so in regexp {#w+} and {$w+} My first question is, are r...

Linq to SQL - Parameters being assigned incorrectly/duplicates

I started with this question: (how-to-compare-liststring-to-db-table-using-linq). Using the solution offered there, it worked for my test with only 5 items in my list. I'm having an issue with this now. I'm getting a SQL error saying "error near 0". After using a LINQ to SQL visualizer, the problem is when Visual Studio is assigning t...

How to rewrite this foreach looping using linq ?

Supposing I have two classes, Customer and Order, where one Customer can have one-or-many Orders associated to it. class Customer { Order[] Orders; } class Order { int OrderId; } If for any given Customer, I want to find all the associated OrderId's, is there an easy way to do that using linq ? Something that gives the same resul...

read attribute using XDocument and LINQ

Hi, I'm trying to parse a rss feed. I can get the elements out, but whatever i try, i cant get the attributes! This is how the rss feed (xml) looks like: <?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0"> <channel> <title>nu.nl - Algemeen</title> <copyright>Copyright (c) 2010, nu.nl</copyright> <link>http://www....

List<> Get next or previous element that satisfys a condition

I am writing a card game and have been using the following method to get the next Player who's turn it is There is a direction to the game which could be forwards or backwards, and it needs to respect this too private Player GetNextPlayer() { int currentPlayerIndex = Players.FindIndex(o => o.IsThisPlayersTurn); ...

LINQ to DataSet, DataTable.AsEnumerable() not recognized

I am brand new to LINQ and am trying to query my DataSet with it. So I followed this example to the letter, and it does not work. I know that my DataTable needs the .AsEnumerable on the end, but it is not recognized by the IDE. What am I doing wrong? Am I missing a reference/import that is not shown in the example (wouldn't be the fi...

LINQ to XML - Selecting elements based on values that exist in other elements

I guess I'm pushing the boundaries of whats capable with LINQ to XML but I'd like to select a group of elements that contain values that match a value contained in another element node. In the XML below I want to select only the "Option" elements that contain values that are also contained in the AvailableOptions" element for a specifi...

Opinion on reuse of db context in Linq

I have a class that uses linq to access the database. Some methods call others. For example: class UserManager { public User[] getList() { using(var db = new MyContext()) { return db.Users.Where(item => item.Active == false); } } public User[] addUser(string name) { using(var db...

Why Skip and Take does not work when passing through a method?

Suppose following codes: IEnumerable<MyClass> MakeQuery() { var query = from m in session.Linq<MyClass>() select m; return query; } List<MyClass> m1() { return MakeQuery() .Skip(10) .Take(20) .ToList<MyClass>(); } List<MyClass> m2() { var query = from m in session.Linq<MyClass>() select ...

Getting stored procedure output parameter with LINQ and Entity Framework

I've created a stored procedure that takes parameters to create a user. If the user already exists it sets the output parameter to 'User already exists' and does nothing more. Now I've mapped this function (InsertNewUser) to my Entity Framework and am calling it like so: context.InsertNewUser(email, name, passwordhash, salt, ???) ...

Multi-line foreach loop in linq / lambda

I am looking for a way to change the following code: foreach (Contact _contact in contacts) { _contact.ID = 0; _contact.GroupID = 0; _contact.CompanyID = 0; } I would like to change this using LINQ / lambda into something similar to: contacts.ForEach(c => c.ID = 0; c.GroupID = 0; c.CompanyID = 0); However that doesn't w...

specify identity value in linq-to-sql

I am trying to set an identity field's value before inserting the new record into the database, I am trying to save having to needlessly re-map all of the FK fields when I know the destination tables are empty already. I had hoped that this question: http://stackoverflow.com/questions/507515/how-do-i-manually-set-an-identity-field-in-li...

Add an entity from another DataContex in linq

I am trying to merge data between two identical schema databases using Linq-to-sql: List<Contact> contacts = (from c in oldDb.Contact select c).ToList(); contacts.ForEach(c => c.CreatedByID = 0); newDb.Contact.InsertAllOnSubmit(contacts); newDb.SubmitChanges(); Merely throws an "An attempt has been made to Attach or Add an entity ...

Help me understand NewExpression.Members.

Does NewExpression.Members inform the LINQ runtime how to map a type's constructor parameters to its properties? And if so, is there an attribute to set the mapping? I'm imagining something like this: public class Customer { public Customer(int id, string name) { Id = id; Name = name; } [CtorParam("id")] public int Id...

Setting EntitySet<t> properties to default using reflection

I am trying to write generic code for detaching a linq class. What I have currently is: public void Detach() { this.PropertyChanged = null; this.PropertyChanging = null; this.Categories = default(EntitySet<Categories>); this.Jobs = default(EntitySet<Jobs>); this.Tasks= default(EntitySet<Tasks>); } This is all fine...

formview with updatepanel vs. jquery/json

Hi, I have a form that's pretty large: about 30 fields, with some very long ones (user's notes for instance) and some can be of variable lenght (a variable-size list of books/title/author). The design calls for the edit view to be in a popup that edits sections of the form at a time. The page must also use ajax so that it never refreshe...

Linq query, select inner query into list?

Hello! I am writing a linq query to select a blogpost, from data in dataContext.Blog_BlogPosts join tagsData in dataContext.Blog_TagToPostConnections on data.BlogPostID equals tagsData.BlogPostID where data.Slug == "asp-programmering" select new BlogPost { Title = data.Title, ...

Cannot implicitly convert type 'System.Data.Linq.ISingleResult<CustomeProcedureName> to 'int'

Sorry for this simple question . I have a Stored Procedure that return an int value , I'm trying to call this sp from my asp.net linq to sql project . int currentRating = db.sproc_GetAverageByPageId(pageId); But i get this error : Cannot implicitly convert type `'System.Data.Linq.ISingleResult<PsychoDataLayer.sproc_GetAverageByPag...