linq

LINQ to SQL Web Application Best Practices

In my experience building web applications, I've always used a n-tier approach. A DAL that gets data from the db and populates the objects, and BLL that gets objects from the DAL and performs any business logic required on them, and the website that gets it's display data from the BLL. I've recently started learning LINQ, and most of th...

initializing properties with private sets in .Net

public class Foo { public string Name { get; private set;} // <-- Because set is private, } void Main() { var bar = new Foo {Name = "baz"}; // <-- This doesn't compile /*The property or indexer 'UserQuery.Foo.Name' cannot be used in this context because the set accessor is inaccessible*/ using (DataContext dc = n...

I'm trying to handle the updates on 2 related tables in one DetailsView using Jquery and Linq, and having trouble with adding and removing related entries

Given two related tables (reports, report_fields) where there can be many report_fields entries for each reports entry, I need to allow the user to enter new report_fields, delete existing report_fields, and re-arrange the order. Currently I am using a DetailsView to handle the editing of the reports. I added some logic to handle repor...

LINQ Quicksort is Unstable Except When Cascading

On page 64 of "LINQ To Objects Using C# 4.0" (Tony Magennis) he states that LINQ's quicksort ordering algorithm is unstable... ...although this is simply solved by cascading the result into a ThenBy or ThenByDescending operator. Huh? Why would cascading an unstable sortation into another sortation fix the result? In fact, I'...

Three level hierarchical data-linq

Hi I have three level hierarchical data. using the statement below i managed to display two level data. I need to extend it to one more level. Current hierachy is Modules-->Documents I need to extend it as Packages-->Modules-->Documents var data = (from m in DataContext.SysModules join d in DataContext.SysDocuments o...

Linq query problem

I have the following information var details = Details.Where(d => d.isActive); I would like to query another table, Authorizations, that has a FK to Details, and get the sum amounts of the authorizations that are contained within the details object grouped by the detail and a FundCode. Details (1 to many) Authorizations Seems si...

Using Linq and C#, how would I categorize a list of list and getting empty categories?

Having the following: var categories = new List<double> {10,20,30,40}; // Note the 40 here... var bundleA = new List<double> {10,20}; var bundleB = new List<double> {20,20,30}; var lots = new List<List<double>> {bundleA, bundleB}; var total = lots.Sum (l => l.Count); var res = from lot in lots from bundle in lot ...

Filtering two arrays to avoid Inf/NaN values

I have two arrays of doubles of the same size, containg X and Y values for some plots. I need to create some kind of protection against Inf/NaN values. I need to find all that pairs of values (X, Y) for which both, X and Y are not Inf nor NaN If I have one array, I can do it using lambdas: var filteredValues = someValues.Where(d=> !(d...

Exposing entities via a nHibernate implementation RIA Services with querystring queries

I once read a blog post and cannot find it anymore. drat! It was about a guy who setup a wcf service (I guess RIA, but could have been something else) exposing the model via IQueryable to the querystring. Sou you could say http://host/articles/123/ratings and you'd get a list (soap or json) of serialized Rating entities (the properti...

Why am I getting "Collection was modified; enumeration operation may not execute" when not modifying the enumerated collection?

I have two collections of strings: CollectionA is a StringCollection property of an object stored in the system, while CollectionB is a List generated at runtime. CollectionA needs to be updated to match CollectionB if there are any differences. So I devised what I expected to be a simple LINQ method to perform the removal. var strDiffe...

How to get an object from a list based upon IEqualityComparer<T>

The Compare method in Linq lets you find by an IEqualityComparer, but I can't find a counterpart method that allows you retrieve an item by the same comparer. Is this really the best way to do it? MyItem myFinderItem = new MyItem(keyField1, keyField2); if (myList.Contains(myFinderItem, new MyEqualityComparer())) { MyItem myRealItem...

LINQ count query returns a 1 instead of a 0

I have the following view:- CREATE VIEW tbl_adjudicator_result_view AS SELECT a.adjudicator_id, sar.section_adjudicator_role_id, s.section_id, sdr.section_dance_role_id, d.dance_id, c.contact_id, ro.round_id, r.result_id, c.title, c.first_name, c.last_name, d.name, r.value, ro.type FROM tbl_adjudicator a INNER JOIN tbl_section_adjudica...

What Sorting Algorithm Is Used By LINQ "OrderBy"?

Evidently LINQ's "OrderBy" had originally been specified as unstable, but by the time of Orca it was specified as stable. Not all documentation has been updated accordingly - consider these links: Jon Skeet on OrderBy stability Troy Magennis on OrderBy stability But if LINQ's OrderBy is now "stable," then it means it is not using a ...

How to convert SQL Statement with TOP, COUNT and GROUP BY to return an object list with LINQ

Hello guys does anyone know how to convert this SQL statement to a LINQ to a List? SELECT TOP(5) COUNT(Tickets.CategoryId), Categories.CategoryName FROM Tickets INNER JOIN Categories ON Tickets.CategoryId = Categories.CategoryId GROUP BY Categories.CategoryName ORDER BY 1 DESC The result would be something like this? public static ...

See if item exists once in Enumerable (Linq)

Given a list... class Item { public Name { get; set; } } List<Item> items = new List<Item> { new Item() { Name = "Item 1" }, new Item() { Name = "Item 1" }, new Item() { Name = "Item 2" }, new Item() { Name = "Item 3" } } List<Item> listing = new List<Item> { new Item() { Name = "Item 1" }, new Item() { Name = "Item 2"...

Equality Comparison with Multiple Instances/IEqualityComparer problems in LINQ

This is similar to my last question; but from a different angle. http://stackoverflow.com/questions/2792393/see-if-item-exists-once-in-enumerable-linq Given the following set of items, and lists containing them... Item 1 Item 2 Item 3 Item 4 Item 5 class Item { string Name { get; set; } } List<Item> available = new List<Item>() { I...

Linq-to-SQL and Performance.

HI, I am developing asp.net mvc site with linq-to-sql we are having 1000 cocurrent users and we are having performance problems. I have found that stackovewflow is also build on linq-to-sql? So can anybody know how they improved performance. Without line performance was good each page are loaded in 3 seconds but after migrating to lin...

linq to entities query for count and group by with join on 2 tables

I need a linq to entities query/ lambda expression for the following statement. Any help is greatly appreciated SELECT at.Name, Count(a.AssetTypeId) as CountofAssets, at.AssetTypeId FROM AssetTypes at, Assets a WHERE at.AssetClassId = 7 GROUP BY at.Name,at.AssetTypeID ...

How do I override the Contains method of IQueryable during a unit test?

So here's the thing: I've got an app that I'm testing that uses LINQ to Entities (EF4/.NET4). The app binds to an implementation of the Contains method that ignores nulls and, due to the way the database is configured, ignores case. That works great. However, when I call into the same methods from my unit tests, I'm passing in a fake co...

Query a List of List of Items in LINQ c#

I am a bit new to LINQ, here is my problem. I have a List of List of Items I like to get the Items which are present in only one List (and if I could get the List in which they are without re-iterating through the "list of list" that would be great). I am trying without success to use the Aggregate / Except / Group keywords in the L...