linq

Is this lambda expression possible?

Thanks to the help with this. Tried this, without luck.. I know from f in list where f.bar == someVar select f can be written as list.Where( f => f.bar == someVar ); Can a similar expression be created from from f in foo from b in f.bar where b.something == someVar select f; ? Edit: Sorry, I forgot f.bar in the second exa...

What is the difference between LINQ query expressions and extension methods

Below are two queries that return the same data. Other then style I am not sure which is better. What factors influence these queries? What are the benefits of using one style over the other? Sample 1 var x = from s in db.Surveys join sq in db.Survey_Questions on s.ID equals sq.Survey_ID join q in db.Questions on sq.Questio...

Aggregate data with LINQ and ASP.NET MVC

Suppose i have a structure Widget id name and a structure called WidgetVoteCount widgetId (FK to Widget.ID) numberOfVotes I want to display instances of Widget along with a corresponding aggregate count on my View WIDGET-TITLE WIDGET-COUNT(which is the sum of WidgetVoteCount.numberOfVotes) Blue Widget 5 Purple Widge...

Quickest way to learn Linq to Entities

I am developing my first ASP.NET MVC application using the Entity Framework. I have no previous .NET experience, although I'm fluent in other OO languages. I am having trouble understanding Linq to Entity query expressions, more specifically its syntax and how to render the results on an ASP page. For example, I am joining three tab...

Dynamic LINQ to Entity Count()

Hi, I'm trying to write the following short line of code in a more generic way. What I want to achieve is a simple count of records, that have changed or were created since a certain date. public int GetChanges(DateTime LastActivityDate) { KHS.Innopas.Web.Library.DataModels.Documentation.DocumentationEntities ctx = ...

NHibernate Linq and DistinctRootEntity

When I execute the following query, I get an exception telling me that 'feedItemQuery' contains multiple items (so SingleOrDefault doesn't work). This is expected behaviour when using the Criteria api WITHOUT the DistinctRootEntity transformer, but when using linq, I expect to get a single root entity (FeedItem, with the property Ads (of...

Linq2SQL dealing with inserts/deletes on table with unique constraints

Hi I have a table that looks like the following: TABLE Foo { Guid Id [PK], int A [FK], int B [FK], int C [FK], } And unique constraint over A, B and C. Now say for example, you insert a row with a fresh PK with with A = 1, B = 1, C = 1. SubmitChanges(), all happy. Now you edit the table. You remove the previous entry, and...

Default Sort Column with Linq to SQL

I am in the process building myself a simple Linq to SQL repository pattern. What I wanted to know is, is it possible to set a default sort column so I don't have to call orderby. From what I have read I don't think it is and if this is the case what would recommend for a solution to this problem. Would the best idea be to use an att...

EntityFramework ObjectQuery<T>.Include With Filter ability

Hi, I'm using ObjectQuery.CreateQuery() to create eSQL query. I want to use the ObjcetQuery.Include to load related data, like Customer => Orders so let the EF to load all the orders for customer at the same time. The issue is that i don't want all the related objects and i want to fetch the result with condition. Any idea? ...

Linq To Sql - How to get # of inserted records.

How can I get the number of records that were inserted? Is there an easier way, with L2S, than count before and count after and taking the difference? ...

User defined filter for linq

Hi, I have form with user defined filters ( combobox with column names, combobox with filter types and textbox with value). How can I dynamicly add user defined filter into LINQ query? Typical query looks like: var qProducts = from p in db.Products where p.IsArchived == false order by p.ProductName select p; I'm using LINQ (...

Hierarchical Database Driven Menu in MVC

I use the code below as an HTMLHelper which gets data from the database and loops over it to display a menu. This is fairly straightforward as you can see however, what if you have a database table using the adjacent model of heirarchies eg/ID, ParentID, OrderID. Easy to see whats going on but recursion is needed to get this data out pr...

LINQ: Get the Parent object properties and a single child property as a flat structure

I have a list of addresses contained in a parent object called Branch. Branch may or may not have these addresses defined and I need to get a flat hierarchy or these branch and address. var x = from p in CurrentBranchList where p.ScheduledForDeletion == false from c in p.Addresses where c.ScheduledForDeletion =...

Slickest way to put objects into separate lists based on a property value.

I have a collection of objects and am curious about the way you would separate them into two lists - one list will have everything of a specific type, the other will have the remainder. One way I thought of doing it is: var typeXs = (from o in collectionOfThings where o.Type == "typeX" select o); var notTypeXs = (from o in collectionOfT...

Where is the best place to start learning LINQ?

I have read my first book on c# and feel completely clueless about LINQ. Please suggest reader-friendly linq material. Thanks ...

How to use generic collections together with LINQ-to-entities

I'm having trouble converting the following SQL-statement to LINQ-to-entities: SELECT l.* FROM locations l WHERE l.id NOT IN (/* array of ids */) In LINQ, I would like to see something like (where myCollection is a generic list of items to be excluded): IQueryable<Location> locationQuery = from l in DataContext.Location ...

Why is .ForEach() on IList<T> and not on IEnumerable<T>?

I've noticed when writing LINQ-y code that .ForEach() is a nice idiom to use. For example, here is a piece of code that takes the following inputs, and produces these outputs: { "One" } => "One" { "One", "Two" } => "One, Two" { "One", "Two", "Three", "Four" } => "One, Two, Three and Four"; And the code: private string InsertCommasAt...

.Net 3.5 database access

Hi everyone, I guess I'm old school, when I architect a project I still think of writing a database layer that includes use of objects like datareaders and datasets... But it seems to me that Microsoft had to come up with better tools since my last project in 2.0, something that would hide all the pipping labor and keep the developer fo...

How do I stop the linq designer overwriting my manual changes?

I like Linq but find that once the designer has created my classes I have to modify them. Then when I change my database and recreate my classes in the designer my changes get wiped. For instance, let's say I have a class called Person. I create this class, add some non database related methods to it (outside of Linq) and then create a ...

500 error while deleting in LINQ

While executing the code below I am getting HTTP 500 Internal Server Error. public bool DeleteData(int dataId, string uploaderName) { using (DataClassesDataContext db = new DataClassesDataContext()) { DataInfo d = db.DataInfos.Where(c => c.DataId == dataId).Single(); db.DataInfos.DeleteOnSubmit(d); db.Sub...