linq

Case Sensitive Where Clause in LINQtoSQL?

I want to check whether a Tag (Case-Sensitive) exists in SQL Server 2005 table using LINQtoSQL. Let's say if a tag 'BEYONCE' exists in tags, then I want that I can add 'beyonce' or 'BeYOnce' but not 'BEYONCE' again. Here is the LINQ query i have written: From t In Context.Tags Where String.Equals(t.Tag, myTag, StringComparison.Ordinal) ...

At what point does LINQ become too terse?

At what point does LINQ become too terse and procedural techniques resorted to? ...

When to use lambda expressions instead of a Where clause in LINQ

I've been really digging into LINQ, and I'm trying to hash out this lambda expression business. I'm just not seeing the benefit of some of the nuances of the syntax. Primarily, it seems to me that a lambda expression is mostly just a different way of using a Where clause. Why wouldn't I just use a Where clause then? Is the lambda expres...

LINQ in ASP.NET -- throws exception when deployed to the server

Hi Everyone, I am connecting to a SQL Server database via LINQ in my ASP.NET application (returning results to a web service). The problem is that it only returns results when I'm running it in debug/test mode - When I publish the service I get an error telling me "Login failed for user 'NT AUTHORITY\NETWORK SERVICE' " So how do I set ...

Does 'And' vs 'AndAlso' matter with linq in vb.net?

Does it matter if you are using 'And' or 'AndAlso' in linq queries in vb.net? I know in normal operations 'AndAlso' is short circuit and so will often be faster, but I don't know if that carries over into linq queries. Does it matter if the linq query is against a database or against an in memory collection? ...

Select Max in Linq to Entities / Entity SQL with type conversion

Hey everyone, I'm trying to create some CRUD functionality for a legacy database and using the Entity Framework as an ORM tool. One table, 'Sites', has a Guid type 'Id' column which is never seen by the users and an integer type 'SiteId' which the users use to identitfy a Site. For god knows what reason the numeric SiteId column is of ...

Linq - calculate multiple averages in one query

I'm trying to convert some SQL queries into Linq to avoid multiple trips to the database. The old SQL I'm trying to convert does: SELECT AVG(CAST(DATEDIFF(ms, A.CreatedDate, B.CompletedDate) AS decimal(15,4))), AVG(CAST(DATEDIFF(ms, B.CreatedDate, B.CompletedDate) AS decimal(15,4))) FROM dbo.A INNER JOIN dbo.B ON B.Par...

C# Linq XML Find Specified element

Please could someone post an example of how to check if an element exists in an xml file using linq? Here is the xml document: <Database> <SMS> <Number>"+447528349828"</Number> <Date>"09/06/24</Date> <Time>13:35:01"</Time> <Message>"Stop"</Message> </SMS> <SMS> <Number>"+447528349828"</Number> <Date>"09/06/24</...

Linq to XML performance - Standalone test vs Very large web application

So I took a specific hot-spot from a very large web application, that does lot of XML processing (adding nodes, adding attributes to nodes based on some logic), then created a standalone test to mock the same situation using Linq to XML (as opposed to XmlDocument) There was 2 to 10 times performance improvement in the standalone test co...

Group by Weeks in LINQ to Entities

I have an application that allows users to enter time they spend working, and I'm trying to get some good reporting built for this which leverages LINQ to Entities. Because each TrackedTime has a TargetDate which is just the "Date" portion of a DateTime, it is relatively simple to group the times by user and date (I'm leaving out the "w...

Linq iterate a control collection

Private Function GetWebDataGridOKButtonId() As String Dim ctls As ControlCollection = _ WebDataGrid1.Controls(0).Controls(0).Controls Dim btn As Button Dim qry = From item As Control In ctls _ Where item.ID = "ButtonOK" _ Select item btn = qry.ToList() Return btn.C...

How can I get the distinct items of the following data structure in C# / LINQ?

I have the following objects: public class Agency { public int ID; public IEnumerable<BusinessUnit> BusinessUnits; } public class BusinessUnit { public int ID; public decimal AmountSpent; public IEnumerable<Client> Clients; } public class Client { public int ID; public decimal AmountSpent; } ...

Why are LINQ extensions written in a very difficult to read way?

I was checking some of the code that make up LINQ extensions in Reflector, and this is the kind of code I come across: private bool MoveNext() { bool flag; try { switch (this.<>1__state) { case 0: this.<>1__state = -1; this.<set>5__7b = new Set<TSource>(this.compare...

Linq To Entities Generating Big Queries

I've been running a trace on some of the queries Linq is generating and they seem very unoptimized and clumsy. I realise you dont know my data structure but is tere anything immidiatly wrong with the following linq query IQueryable<Tasks> tl = db.Tasks .Include("Catagories") ...

Persisting Lazy Loaded Properties

I am using a simple repository pattern and have objects with a LazyList such as: public class Promotion { public int Id { get; set; } public string Name { get; set;} public LazyList<Site> TargetSites { get; internal set; } // implemented as a LazyList } This works great for getting the items but I'm wondering what is usual...

How do I Aggregate multiple IEnumerables of T

Given.... Public MasterList as IEnumerable(Of MasterItem) Public Class MasterItem(Of T) Public SubItems as IEnumerable(Of T) End Class I would like a single IEnumerable(Of T) which will iterate through all SubItems of all MasterItems in MasterList I would like to think that there is a Linq facility to do this, or an extension me...

Linq To XSD at runtime.

Hi, I'm trying to get the .NET object representation of a certain XSD(element) at runtime. How can I use Linq to XSD to do that in runtime as opposed to design time? Thanks, --Ran. ...

Force Linq to not delay execution

In fact, this is the same question as this post: how-can-i-make-sure-my-linq-queries-execute-when-called-in-my-dal-not-in-a-delay But since he didn't explain why he wanted it, the question seems to have been passed over a bit. Here's my similar-but-better-explained problem: I have a handful of threads in two types (ignoring UI thread...

How can I add an extra item in this LINQ query?

I have the following objects. An IEnumerable<Agency> Agencies, which then contains BusinessUnits and BusinessUnits contain clients: public class Agency { public int ID; public IEnumerable<BusinessUnit> BusinessUnits; } public class BusinessUnit { public string Name; public int ID; public decimal Amoun...

Most efficient way to select a specific object type in Dictionary and delete it.

Okay I have a series of objects based on a base class which are stored randomly in a Dictionary object. e.g. class TypeA { public int SomeNumber {get; set;} public void SomeFunction() } class TypeB : TypeA { public string SomeString {get; set;} } class TypeC : TypeA { public bool StuffReady() } Dictionary listOfClasses <...