linq

Which methods to close a compiled query

As we know you can't add an extra clause like .Where() or .First() to a compiled query, because that changes the query and forces a recompile. What I would like to know is which methods can be used to "close" a compiled query. I know most people use either .AsEnumerable() or .ToList(), but which other methods work as well? Can I use .A...

LINQ: How to join nested Lists in an ObjectCollection

Hi, is it possible to define a LINQ statement for the following problems WITHOUT using a foreach loop? public class GroupObject { public String name; public List<String> letters; public void test() { List<GroupObject> myGroups = new List<GroupObject> { new GroupObject { name="test1", ...

Is there a good source that gives an overview of linq optimizations?

Linq does a lot of clever things such as returning the result of the Count-property using the Count() method on a IList. Is there a good source that gives an overview of this optimizations? It would be very interesting because as before I knew the above, I never used Count() and thus often returned a List<T> than only an IEnumerable<T> ...

Funky Sql Generated using SubSonic Simple Repository, LINQ and ASP.NET MVC

I have the following code: if (collection["Filter"] == "2") { presentations = presentations.Where(x => x.Speaker.FirstName.StartsWith("B")). OrderBy(x => x.Speaker.FirstName); } this generates the following sql: SELECT [t0].[Description], [t0].[EventId], [t0].[Id], [t0].[PresentedOn], [t0].[Slug], [t0].[SpeakerId], ...

Linq to nhibernate question

Hi, I'm trying to create a query using linq 2 nhibernate which generate a sql like: select * from table where id in (1, 2, 3, 4) At the moment I have this code: var vouchers = Session.Linq<Voucher>() .Where(x => campaignIds.Contains(x.VoucherGroup.Campaign.Id)) .ToA...

How can I create a dynamic select expression in LINQ?

What I currently have looks a bit like this: if(userLikesBananas) { return from fruit in basket select new Fruit { AteBanana = Bowl.Any(b => b.OwnedBy == user && b.Contains(fruit) && fruit.Type == FruitType.Banana), ... ... //lots of properties ...

Linq query in C# is not working as expected.

I have reproduced my problem in a console app. Following is the code. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LinqSample { class Program { static void Main(string[] args) { string query = "Maine, Maryland, Massachusetts, M"; var qu...

Chain together multiple complex WHERE clauses in LINQ to SQL

This is the pseudo-SQL I want to generate: SELECT * FROM Table WHERE Column1 = @Value1 OR Column2 = @Value2 The problem is, sometimes the second one should not be included. I was hoping to chain together .Where() clauses like so: var query = context.TableName; query = query.Where(t => t.Column1 == value1); if (NeedsToBeIncluded(valu...

LINQ 2 SQL Method for all tables

My Title may be slightly off but here is what I am trying to do. I have a L2S Method that would be for every table that I would like to write once. This is to set a soft lock column where I will also need a Read and UnLock method. Here is what I have so far: public static void LockRow(string TableName, int TablePrimaryKey) { ...

LINQ - Get all items in a List within a List?

Hey all, I'm currently working my way through the learning curve that is LINQ and I could really use some assistance. I don't know if what I want is possible, but if I had to wager, I bet it is. I currently have a list of objects called _tables and each of these objects has within it another list of objects exposed through the propert...

Traversing ASP.NET HTML Elements using LINQ

Guys, Assuming I have the following ASP.NET HTML Code: <html> <head><title>Test Page</title></head> <body id="bodyID"> <asp:Label runat="server" id="lbl1" text="First Name:" /> <asp:TextBox runat="server" id="txt1" /><br /> <asp:Label runat="server" id="lbl2" text="Last Name:" /> <asp:TextBox runa...

ADO.NET EF 4 vs. DataSets (of any kind)

Hello, I've been reading updates on Data Sets vs. Other ORM's like ADO.NET Entity Framework, but a lot of them refer to the older version, so with EF 4 as an option today, what is people's opinion for data sets vs. EF 4, which is better, worse? I like EF 4 because: The designer finally works well. The variation of model options (POC...

Performing a LINQ query with optional search clauses

I have a page where you can search for people. They can either get a list of all people, or filter it by certain criteria such as first or last name. So far, I have been trying trying to use the technique detailed in this question. So my code looks like string firstname=... string lastname=... var people=from p in People.All() ...

LINQ to Entities Question - All objects where all items in subcollection appear in another collection?

Hey all, Hopefully I can explain this to where it make sense, but I'm trying to get a list of objects out of a master list using a speicific and complex (complex to me, at least) set of criteria. I have a Class called TableInfo that exposes a List of ForeignKeyInfo. ForeignKeyInfo has a string property (among others) called, Table. I...

LINQTOSql Missing parameter question

I have a long LinqtoSQl query in which several parameters I'm not forcing the user to specify anything. I started using a Select Case statement that would test rather or not a parameter string's length > 0 or if it's an int > 0. But I realized that I would have to test for each possibility and create queries based on each. I did some se...

linq how to process each element?

I can never remember. How do i process each element in a string? I want to write stringblah.Split('/n', Split('\n', StringSplitOptions.RemoveEmptyEntries)) .Each(s=>s.Trim()); ...

LINQ to SQL Query for Many-to-Many relationship

I am moving an old ASP.net (C#) application from plain SQL queries to LINQ to SQL and am having some trouble with some of the more complex queries. In this case, I am trying to get a list of employees who have a certain set of skills. The user picks the skills to search for and the id's are supplied to the method that does the work. In t...

Serializing result of a LINQ IEnumerable

I have a simple value type: [Serializable] private struct TimerInstance { public TimerInstance(string str, long nTicks) { _name = str; _ticks = nTicks; } private readonly string _name; private readonly long _ticks; public string Name { get { return _na...

Nested select top 1 in Linq

Hey, having some problems figuring this one out. select *,(select top 1 chicken_nr from chicken_photo where chicken = code order by [sort]) as Photo from Chicken Code is a column in Table Chicken Basically getting the cover photo for this chicken. To make it clearer, I want it to return multiple rows from table Chicken. But only a s...

What is the best way to find all dependent children in a IEnumerable collection

I have a database with 2 tables: Items ItemDependencies Items has key of ID ItemDependencies have two columns: ItemId, and DependsOnItemId I conver this to a collection: IEnumerable<Item> items = GetItems(); each item has a: Dependencies property which is a List<Item> So i want to filter the initial items list to: Given a ...