linq

Why doesn't deferred execution cache iterative values?

Take the code below, adapted from this question: //Borrowed from another question because its a simpler example of what happened to me. IEnumerable<char> query = "Not what you might expect"; foreach(char vowel in "aeiou") { query = query.Where(c => c != vowel); } foreach (char Output in query) { System.Out.WriteLine(Output); } ...

Temporary Entities with Entity Framework

I have an EntityObject named Pathway which relates directly to data in the pathway table. My database also stores rules for Pathway customisations. What I would like to do is create a Pathway object in my code that is the resultant of the Pathway + PathwayCustomisations. The resultant should never find it's way back to the database, it i...

How to get position inside a select statement

Hi, I am using LINQ like from a in b.Descendants("SomeNode") select new MyClass { isfirst= false, islast = false, }; How can I get the position of the element here? I basically want to know which one is first and which one is last element. ...

Getting a multi-dimensional result out of a stored procedure with LINQ to SQL

I am using LINQ to SQL to call a stored procedure with a single result set (I have found a lot of information about handling multiple result sets, but that is not what I am doing). The result set is actually all columns from 3 tables joined together, and I have LINQ to SQL entities for those 3 tables. The schema is something like this: ...

What is the difference between Converting and Unboxing?

In the DLR's LINQ Expressions, what is the difference between this: Expression.Convert(SomeVariableExpression, typeof(T)); and this: Expression.Unbox(SomeVariableExpression, typeof(T)); The documentation on this seems a bit sketchy. And more to the point, which one of these is equivalent to this C# code: (ClassA)InstanceOfClassB ...

Linq-to-entities Include method doesn't load related records.

Hi, I've got 3 tables in my DB: InvoiceDetailLineType, InvoicingCategory, CalculationRule. InvoiceDetailLineType has foreign keys to both InvoicingCategory and CalculationRule. I want to retrieve a list of all InvoiceDetailLineType objects for a given category and include the related CalculationRule and InvoicingCategory objects. Her...

Linq query, how to build nested objects from single table

Hi there, I have a single table and I need to build a bunch of nested objects based on the single table. Data: PointA PointB Month Time Price 1 2 11 11:00 10.99 1 2 12 11:00 9.99 Objects are POINTS {PointA, PointB, Details} Details {Month, ExtraDetails} ExtraDetails {Time, Price} I want to avoid having loads of loops and if st...

How to use LINQ to compile a lambda expression to custom SQL or otherwise?

Starting with this code: int Count(Func<MyClass, bool> conditions) { // ... } And I want to call it like so: int c = Count(foo => foo.bar == 5 && !foo.blah); How do I then write the count function so that it ends up like so: int Count(Func<MyClass, bool> conditions) { // what goes here so that I get this: string sql = ...

Overlay/Join two collections with Linq

I have the following scenario: List 1 has 20 items of type TItem, List 2 has 5 items of the same type. List 1 already contains the items from List 2 but in a different state. I want to overwrite the 5 items in List 1 with the items from List 2. I thought a join might work, but I want to overwrite the items in List 1, not join them toge...

ASP.net gridview sorting with linq result

hey guys I'm in a bit of pickle here. up till now I have a linq query that fills a datagrid perfectly with filterconditions. however, when I try to implement sorting I fail. I have the following code behind. it catches the start of the sort. protected void gvServers_Sorting(object sender, GridViewSortEventArgs e) { if (e.SortDire...

Efficiently "drilling down" into XML tree with xlinq?

I'm writing a program to parse some third-party XML files. The structure is something like... <CharacterSheet> ... <StatBlock> ... <Stat> ... <alias /> ... </Stat> ... </StatBlock> ... </CharacterSheet> I'm using this in to get some practice with linq, and I'm fining I have to write some really ugly chained querie...

Linq group by with a sub query

Hello, I have an object that contains these three properties: ID, DATE and NAME and want to query it with linq yielding the same results as if written in the sql statement below. SELECT ID, MAX(DATE), NAME FROM TABLE GROUP BY ID Here is what I have so far with linq: var grouped = from a in table group a by a.ID into g select new fo...

What is the equivalent C# 3.0 Linq to SQL for this?

Old VB6 Code that needs to be converted: 'DB Connection Set Conn = New ADODB.Connection Conn.ConnectionString = sConn Conn.Open sConn Conn.BeginTrans 'Recordset Set rsPrice = New ADODB.Recordset rsPrice.CursorLocation = adUseClient rsPrice.CursorType = adOpenKeyset rsPrice.LockType = adLockBatchOptimistic rsPrice.ActiveConnection = Con...

LINQ vs regular enumeration

When you have some enumeration methods in some of your types (i.e. a custom collection), is it better to use LINQ syntax or just old school enumeration (for/foreach)? Using .NET 3.5 is a given. I am asking this for maximum performance and readability. The application is also designed to be parallel. Which one would you favor? ...

Populating a generic ViewModel for building reports

I need to generate several reports, and many of them will simply be a table of entities from my database. The entities could be of any type, and I won't always need the entire entity. My current approach is to create a ViewModel that contains a field of type List<List<string>>, which represents my table, where each row is a List of cel...

Where can I persist what company a user belongs to with ASP.NET MVC and MembershipProvider?

When a user logs on to my ASP.NET MVC application I need to persist what company that user belongs to. The company they belong to will determine what database ~all~ their queries come from so it is important for me to query for their company as soon as they log in and persist it so I don't have to perform this lookup again. Where/How ...

In what scenarios is LINQ best applicable?

In what scenarios is LINQ best applicable? Would it be good "sense" to suggest to use LINQ to query all kinds of collections? ...

Check if date is this date or bigger

Hello. I trying to check if a date is this date or bigger inside my linq query with boolean. But it's not working like i want. This my code public bool CheckMonth(int month) { if (month > System.DateTime.Now.Month) { return true; } else if (month == System.Dat...

C# LINQ: What is the difference between a Pull model and a Push model ?

I am currently reading Albahari's C# 3.0 in a Nutshell book and on page 292 it says this about LINQ: LINQ follows a demand-driven pull model, rather than a supply-driven push model. What does the above statement mean? And what is the difference between a pull model and a push model ? ...

ASP.net gridview datasource null when sorting

Here we are again with the next problem in sorting. this is a follow up question to this question I have now made a type to contain the data I need. however, when I try to fetch the data FROM the gridview it returns null, which means I can't sort anything that is not there in the first place... any ideas why this returns null ... IEn...