linq

How to get list of intermediate sums in a functional way? Using LINQ?

Given a list of objects i need to return a list consisting of the objects and the sum of a property of the objects for all objects in the list seen so far. More generally given var input = new int[] {1,2,3} I would like to have the output of // does not compile but did not want to include extra classes. var output = { (1,1), (2,3)...

How would I write this as a single LINQ query?

Using the following logic, what would the correct syntax be for a single LINQ query? If Branch is Service, I want its parent, otherwise I want Branch. Can you critique my attempt and let me know how I can improve it? int branchId = 21; var t = ctx.BranchInfos.Single(p => p.BranchID == branchId ); if (t.Type == BranchType.Service.T...

Passing a anonymous type to function.

I have tables with either many columns or a column with a large data set (XML doc), I need to load information from the table quickly (status info) into a class. I notices that Linq allows you to select columns as such...: Using an anonymous type: var query = (from name in some.Table select new { ColumnName = name.ColumnNam...

Null value cannot be assigned - LINQ query question

I have the following LINQ query: DataClassesDataContext dc = new DataClassesDataContext(); var query = from contact in dc.Contacts select new { ContactId = contact.ContactId, LastName = contact.LastName, FirstName = contact.FirstName, Addresses = co...

Why am I getting an InvalidOperationException with this Linq to Sql method?

Hi all. When Executing the following linq to sql statement: var stuff = from l in _db.SqlLinks select new { Link = l, Rating = (from v in l.SqlLinkVotes where v.Tag == tagId ...

Use ResourceReader to create a HybridDictionary of resources.

I'm using a ResourceReader to read an embedded resx resource and I want to store it at a class level in a member variable. I'm want to store it as a HybridDictionary but don't see an easy way of doing it. Class member private IEnumerable<DictionaryEntry> dictionary; Class initalize Assembly asm = Assembly.GetExecutingAssembly(); Str...

What is the biggest mistake people make when starting to use LINQ?

What are the fundamental misunderstandings people have when they first start using LINQ? For instance, do they think it is one thing when it is really something else? And, are there some best practices to employ to avoid these mistakes? ...

ASP.NET MVC query with optional field and value

What I'm trying to do is provide a generic search capability on a table. So the user is presented with all data in a table, they enter some text to filter on, and voila, the table is now filtered on all results that match that entry. I have this working with a single field: public ActionResult Index(string user_name) { var dataCon...

What are some clever uses of LINQ?

What are some clever uses for LINQ outside of LINQ to SQL? Have you found any problems that LINQ made a whole lot easier to solve? Please post examples. ...

Why is LINQ to SQL inserting duplicates into my table?

I have a web service that retrieves reviews from my database and if not enough reviews are returned, it goes to a secondary source and gets them there and caches them in my database. I have recently noticed that it has been creating duplicates in my database despite me having primary keys to prevent it. I was thinking that maybe when I m...

Examples on when not to use LINQ

This is sort of a follow up on this question I read: http://stackoverflow.com/questions/777400/what-is-the-biggest-mistake-people-make-when-starting-to-use-linq The top answer is "That it should be used for everything." That made me wonder what exactly that means. What are some common examples where someone used LINQ when they should ...

LINQ to Objects question

I am writing a method that is passed a List<AssetMovements> where AssetMovements looks something like public class AssetMovements { public string Description { get; set; } public List<DateRange> Movements { get; set; } } I want to be able to flatten out these objects into a list of all Movements regardless of Description and am tr...

Aggregate multiple arrays into one array (Linq)

I'm having trouble aggregating multiple arrays into one "big array", I think this should be possible in Linq but I can't get my head around it :( consider some method which returns an array of some dummyObjects public class DummyObjectReceiver { public DummyObject[] GetDummyObjects { -snip- } } now somewhere I have this: public ...

ASP.NET: Getting all the page's control as IEnumerable

Hi, I'm trying to use the page control's collection with LINQ. Whereas this works: dim l = Me.Controls.OfType(Of TextBox).AsQueryable() the following return an ArgumentExceptionError: dim l = Me.Controls.AsQueryable() I need all the controls. Any help? Thanks ...

Datacontext ExecuteCommand paramters IN statement

What is the best way to run a custom sql statement using IN from a c# linq to sql datacontext? I have tried: db.ExecuteCommand( "UPDATE tblCard SET used = 1 WHERE id IN ({0}) AND customer_id = {1}", Request.Form["ids"], customer_id ); Which is fine for 1 item passed through the form, but if i get posted through for example "2,1"...

How to dynamically add OR operator to WHERE clause in LINQ

I have a variable size array of strings, and I am trying to programatically loop through the array and match all the rows in a table where the column "Tags" contains at least one of the strings in the array. Here is some pseudo code: IQueryable<Songs> allSongMatches = musicDb.Songs; // all rows in the table I can easily query this ta...

VB.Net Linq - How to append a where clause?

Pretty new to VB.Net. Having a bit of trouble here with something I though should be simple. Keeping it simple. Let's say i have a Document table with "Name" that I want to search on (in reality there are several other tables, joins, etc ..). I need to be able to build the query where clause based on string values passed in. Example - ...

How do you use Linq to connect tables in different databases?

I'm a bit of a Linq newbie, and I couldn't find any documentation to help me with what seems to be a pretty trivial problem - so your help will be much appreciated! I have a table Table1 in database DB1, which has a "pseudo" foreign key Table2ID to table Table2 in database DB2, on the same server. "Pseudo", because obviously I can't ha...

How would one do an inner join in linq to entities?

Given two tables, customer and orders, how would one do a linq query to entities to find any customers with open invoices started before a certain date? ...

LinqToXML: Getting elements with given value

I have this xml-file: <objects> <object> <value>test</value> </object> <object> <value>foo</value> </object> <object> <value>bar</value> </object> </objects> Now, I want to query this xml, and retrieve all the object-elements where the text in the value-element = "foo" Is there a easy way of doing thi...