linq

Use Linq to find repeating elements

Let's assume I have a List with objects of type "Value". Value has a Name property: private List<Value> values = new List<Value> { new Value { Id = 0, Name = "Hello" }, new Value { Id = 1, Name = "World" }, new Value { Id = 2, Name = "World" }, new Value { Id = 3, Name = "Hello" }, new Value {...

Query Notation for Select overload with index ...

There is a Select overload that adds an index to each element of a sequence : Dim Letters = new string() {"a","b","c","d","e"} Dim IndexedLetters = Letters.Select(function (aLetter, index) new with {.Index = index + 1, .Letter = aLetter}) ' For LINQPad users : IndexedLetters.Dump Can this query be written in Query Notation ? ...

Entity Framework Many-To-Many + Count

I have three tables, Professors, ProfessorStudent, Student. I want all Professors + How many Students each Professor have. I can do this: context.ProfessorSet.Include("Student") context.ProfessorSet.Include("Student").ToList() will read all three tables. But i dont wanna get Student table, I want that Linq just get "Professor Table...

JOIN and LEFT JOIN equivalent in LINQ

I am working with the following SQL query: SELECT a.AppointmentId, a.Status, a.Type, a.Title, b.Days, d.Description, e.FormId FROM Appointment a (nolock) LEFT JOIN AppointmentFormula b (nolock) ON a.AppointmentId = b.AppointmentId and b.RowStatus = 1 JOIN Type d (nolock) ON a.Type = d.TypeId LEFT JOIN AppointmentForm e (nolock) ON e.Ap...

How can I make sure my LINQ queries execute when called in my DAL, not in a delayed fashion?

I have a DAL that is composed of a bunch of methods that perform LINQ queries on my database. How do I ensure that before returning say an IEnumberable or something similar from the database, I ensure that the LINQ query is execute then, not in some delayed fashion only to be executed when the result is used? I know I can call .ToList(...

Linq and ObservableCollection

Hello everybody, I have a problem with Linq and ObservableCollections in my WPF application. Context of the problem: I've created a very simple SQL database with two tables: User and BankAccounts. The User Table has an one-to-many relationship with the BankAccounts Table. Next I've created Linq-to-SQL dataclasses, which worked fine ==...

Linq to Entities - Increment a column using primary key

I have this code: Message message = new Message(); message.Id = 5; message.EntityKey = context.CreateEntityKey("MessageSet", entity); message.Votes++; context.Attach(entity); context.ObjectStateManager.GetObjectStateEntry(entity.EntityKey).SetModifiedProperty("Votes"); Save(); But of course, Votes initializ...

Filter child elements in an ASP.NET Linq-to-Entities

I have the following type of situation: TABLE Customers ( CustomerID int, etc... ) TABLE Orders ( OrderID int, CustomerID int, Active bit, etc... ) I am using this in an ASP.NET MVC web application using Linq-to-Entities. I want to select all Customers and populate the Customer.Orders navigational property, a...

DataContext reusing connection

Hi, this is a simple question that you might find out if you have been in this situation before: Imagine that I have a loop and inside it I create an instance of a DataContext and I perform some queries to the database. The question is... Is the DataContext opening a Connection only the first time and reusing it or a new connection to ...

Error when trying to filter on a property of an entity inheriting from another in Entity Framework

When I have entity B inherit from entity A using table-per-type for storage and try to write a Linq query that filters on a property on B, for example Function(b) b.name="Joe" I get the error The specified type member 'name' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation proper...

Compare dataset or a better idea

How do I compare values of one data set from another. 1st dataset["proper records"](coming from sqlserver, with column name [id],[subsNumber]), 2nd dataset["proper and inproper records"](coming from progress database, with diffrent coulums except 1 which is[subsNumber]). how do I go and make another dataset which has all the [subsNumbe...

Linq Select Statements - Where Not In

I'm trying to write up the LINQ statement that is equivalent to: select e.EmployeeID, EmployeeName = e.FirstName + ' ' + e.LastName from Employees e where e.EmployeeID not in ( select EmployeeID from Managers ) I think I'm pretty close with the following: from e in Employees where e.EmployeeID != // This is where I'm lost ( ...

Linq To Entities Query Oddity

I keep getting an exception about Linq to Entities not supporting certaion query expressions like this: MyDataContext db = new MyDataContext() Brand = db.Brands.First(b => b.BrandId == Int32.Parse(brandIdString)) I'm not attempting to pass along the string parsing on to the entity store, I just want to parse that string into an integ...

How to specify which columns can be returned from linq to sql query

I'm trying to only return a few columns from a linq to sql query but if I do, it throws the exception: Explicit construction of entity type 'InVision.Data.Employee' in query is not allowed Here's the code: return db.Employees.Select(e => new Employee() { EmployeeID = e.EmployeeID, FirstName = e.FirstName, LastName = e....

Most efficient way to auto-hookup a foreign key in LINQ

I have two tables for tracking user sessions on my site. This is a gross oversimplification btw : Campaign: campaignId [int] campaignKey [varchar(20)] description [varchar(50)] Session: sessionDate [datetime] sessionGUID [uniqueidentifier] campaignId [int] campaignKey [varchar(20)] ...

Why do people use linq to sql?

Given the premise: There are competent sql programmers (correlary - writing sql queries are not an issue) There are competent application developers (correlary - there is simple/strong/flexible architecture for handling connections and simple queries from code) Why do people use linq to sql? There is overhead added to each transact...

Access Linq result contained in a dynamic class

I'm using DbLinq which should be the equivalent of Linq2SQL for this question. I'm need to generate a Linq2SQL query where I specify the columns I want returned at runtime. I can achieve that using the Dynamic Linq extension methods but I can't work out how to extract the result. string someProperty = "phonenumber"; string id = "1234"; ...

Using LINQ/Lambdas to copy a String[] into a List<String> ?

What's the correct C# syntax to do the following using sexier syntax? string[] arr = ///.... List<string> lst = new List<string>(); foreach (var i in arr) { lst.Add(i); } I feel like this could be a one liner using something like: arr.Select(x => lst.Add(x)); but I'm not quite bright enough to figure out the right syntax. ...

How does OrderBy in LINQ works (behind the scenes)?

I already know that LINQ works by evaluating expressions and iterating one by one thru them (kinf of like a pipeline), however there are certain operations like OrderBy that need to be buffered since sorting needs to analize all the data at once to do the sort. I am interested in knowing behind the scenes how is this data buffered in LI...

How can I make this LINQ query cleaner?

I have recently written a LINQ query to get a Dictionary containing the last 6 month's placement amounts. It is returning a Dictionary of Month string - Decimal Amount pairs. It seems kind of cludgey. Any of you LINQ masters out there able to help me refactor this to make a bit cleaner? /// <summary> /// Gets the last 6 months of Pla...