linq

Nested Group by in LINQ

How to create a nested group by query in LINQ for the below table: Code Mktcode Id 1 10 0001 2 20 0010 1 10 0012 1 20 0010 1 20 0014 2 20 0001 2 30 0002 1 30 0002 1 30 0005 I want to get the above and convert it to the data structure of the sort Dictionary< Code, List<Dictionary< Mktcode, List<Id>>>>. So the result of this dicti...

Using LINQ with stored procedure that returns multiple instances of the same entity per row

Our development policy dictates that all database accesses are made via stored procedures, and this is creating an issue when using LINQ. The scenario discussed below has been somewhat simplified, in order to make the explanation easier. Consider a database that has 2 tables. Orders (OrderID (PK), InvoiceAddressID (FK), DeliveryAddre...

If Else in LINQ

Is it possible to use If Else conditional in a LINQ query? Something like from p in db.products if p.price>0 select new { Owner=from q in db.Users select q.Name } else select new { Owner = from r in db.ExternalUsers select r.Name } ...

MethodInfo for EntityCollection instead of Queryable

I am manually creating the equivalent lambda: var function = p => p.Child.Any(c => c.Field == "value"); I have a MethodInfo reference to the "Any" method used with Expressions built in code. MethodInfo method = typeof(Queryable).GetMethods() .Where(m => m.Name == "Any" && m.GetParameters().Length == 2) .Single().MakeG...

If all my sql server database access is done thru stored procedures.....

If all my sql server database access is done thru stored procedures, and I plan on continuing that practice, is using linq2sql and/or the entity framework for future projects an unnecessary layer of complexity that doesn't add much value? Related question: is Microsoft trying to steer developers away from relying on stored procs for dat...

Use linq to generate direct update without select

G'day everyone. I'm still learning LINQ so forgive me if this is naive. When you're dealing with SQL directly, you can generate update commands with conditionals, without running a select statement. When I work with linq I seem to follow the pattern of: Select entities Modify entities Submit changes What I want to do is a direct u...

how to set auto increment value=true in LINQ

Hello all, I have a table with names 'Employee' with four field EmployeeId,Name,Address,Age I have set the EmployeeId is Primary key.I want that EmployeeId field will be incremented automatically whenever adding any new records. Codebehind for insert is: { DataClassesDataContext db = new DataClassesDataContext(); Employee ...

GridView paging on custom collecton

I am using Paging on a GridView that is tied to a List. But i use this code, it does not work. What is the correct way. i am not using objectdatasrc protected void myGV_PageIndexChanging(object sender, GridViewPageEventArgs e) { List<Employee> p = ReturnEmplColl(); GridView1.DataSource = p.Paging(1, e.NewPageIndex); G...

Convert Anonymous Type to Class

I got an anonymous type inside a List anBook: var anBook=new []{ new {Code=10, Book ="Harry Potter"}, new {Code=11, Book="James Bond"} }; Is to possible to convert it to a List with the following definition of clearBook: public class ClearBook { int Code; string Book; } by using direct conversion, i.e., without looping throug...

Linq to SQL: execution order when calling SubmitChanges()

I have 2 related database tables which in simplified form look like this Product( product_id, name ) ProductSpecs( spec_id, product_id, name, value ) Foreign key is set via product_id field and ProductSpecs table has a unique constraint on (product_id, name) pair. Now in my ASP.NET MVC application when user edits product...

LINQ to SQL Insert

Hi, i'm using LINQ To SQL to perform an insert via db.table.InsertOnSubmit(). I'm wondering if there is a way to reproduce the T-SQL version of the 'where not exists (select etc etc) begin insert into etc etc end' as one single query? Thanks, Martin ...

LINQ Sorting

Hello, I am sorting a GridView bind to a ananymous type. protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { List<object> lb = (List<object>)ReturnAnony(); // Not working // ?? What to write here } object ReturnAnony() { var anony = // I create the ananymous type and return return anony; } ...

dynamic property access for generated LINQ Classes

How can I access dynamically properties from a generated LINQ class ? Cause I would like to be able to customize the displayed table columns where Partner is the LINQ Class generated from a SQL Server Database Table. <table class="grid"> <thead> <tr> <% foreach (Column c in (IEnumerable)ViewData["columns"]) { %> <th><%= ...

SQL to L2S Translation Help

So here is the original query SELECT SUM(PickQty), SUM(ReqQty), AssignmentID, StopID FROM PickRequest GROUP BY AssignmentID, StopID in LINQ from a in dbReqs group a by new { a.AssignmentID, a.StopID } into pr select new { Assignment = pr.Key, StopID = pr.Select(s=> s.StopID), PickQty = pr.Sum(p=> p.PickedQty), Count = pr.Sum(c => c.Re...

Improve coding with List Intersect using Net 3.5

I've a class that has a list of IDs with a function for adding new IDs from a list of objects. I've got this resolved, but I'm sure this can be done with much less code. Public Class Page Private _IdList As List(Of Integer) Private _HasNewItems As Boolean = False Public Sub AddItems(ByVal Items As List(Of Item)) Dim itemsI...

Linq to Entities, EntityReferences and DataGridViews

I am trying to select certain fields from my entity to be used as the datasource for a datagridview, but I haven't been able to make it work. Is such a thing possible? For example, I have a Customers entity that contains several entityreferences. I want to take fields from the customers entity and from within those entityreferences an...

LINQ to SQL using GROUP BY and COUNT(DISTINCT)

I have to perform the following SQL query: select answer_nbr, count(distinct user_nbr) from tpoll_answer where poll_nbr = 16 group by answer_nbr The LINQ to SQL query from a in tpoll_answer where a.poll_nbr = 16 select a.answer_nbr, a.user_nbr distinct maps to the following SQL query: select distinct answer_nbr, distinct user_n...

Linq to Sql Deferred Loading

If you do not plan to use lazy loading, should deferred loading be explicity set to false within a "using" block? It seems to me that child objects are being loaded by dataContext within the block. ...

What is the LINQ to XML equivalent for this XPath

I wondering what the "best practice" way (in C#) is to implement this xpath query with LINQ: /topNode/middleNode[@filteringAttribute='filterValue']/penultimateNode[@anotherFilterAttribute='somethingElse']/nodesIWantReturned I would like an IEnumerable list of the 'nodesIWantReturned', but only from a certain section of the XML tree, d...

Using Linq for ObjectDataSource: How to transform datetime using ToShortTimeString?

I am accessing a business class using an ObjectDataSource and trying to produce output that makes sense to the user. The return values describe a Class (as in Classroom and teaching, not software). I would like to show the time of the class as a range like this: "9:00 AM - 10:00 AM". This is the Linq Query I am using to pull the data:...