linq

In LINQ, what is the main difference/usefulness between .Any<> and .Where<>

For example, if I had a Linq to SQL data context, or if I had ADO.NET Entity Framework entities that mapped to a database table, and I want to select a single Customer... What is the difference between: MyDatabaseContext.Customers.Any(c => c.CustomerId == 3) and MyDatabaseContext.Customers.Where(c => c.CustomerId == 3) Are both e...

How to create more complex models from many tables in asp.net mvc, linq

Hi All, I know that there are a few questions like this on SOF, but I was interested if someone could succintly answer it with examples. I am new to linq and mvc, and although a lot of it is sticking, I am finding it hard how to imagine that I can do lots of stuff with data. Here is an example. Lets say that I have 5 tables, some of w...

Listing expressions for OrderBy

I would like to be able to store a list of expressions to execute with IQueryable.OrderBy at a later time, something like: List<Expression<Func<MyType, object>>> list = new List<Expression<Func<MyType, object>>>(); list.Add(x => x.Date); list.Add(x => x.ID); IOrderedQueryable<MyType> qry = query.OrderBy(list[0]).ThenBy(list[1]); ...

Populate DataTable with LINQ in C#

I have a method in my app that populates DataTable with the data using the following code: DataTable dt = this.attachmentsDataSet.Tables["Attachments"]; foreach (Outlook.Attachment attachment in this.mailItem.Attachments) { DataRow dr = dt.NewRow(); dr["Index"] = attachment.Index; dr["DisplayName"] = String.Format( ...

C# retrieve where clause on linq

Hello, Is there a way to build a linq query in order to use it later or to display/print it (and more specifically, the where clause) ? ...

Creating Key/Value collections using LINQ To Objects

I'm trying to use LINQ To Objects to create a query that will give me files, indexed by filename with a value mapping to their binary data as byte[]. However I can't find a 'neat' way to do this. I'm hoping to get something like a Dictionary<T,K> output. Here's what I have so far. Example delimFileNames="1.jpg|2.jpg" //Extract filenam...

C# Linq syntax help - How can I exclude two values?

Hi all, I have this linq query that works well (although it may be written better, pls say so if you notice something) var qry = BenefitCodes .Where(b => b.BenInterest != 'E' && (b.BenProductLine == CoverageProductLine || b.BenProductLine == null) ) .Select(b => b) .OrderBy(b => b.BenDesc); A new requirement came ...

Data Annotation Ranges of Dates

Hi Is it possible to use [Range] annotation for dates? something like [Range(typeof(DateTime), DateTime.MinValue.ToString(), DateTime.Today.ToString())] Thanks Davy ...

Entity Framework T-Sql "having" Equivalent

How can I write a linq to entities query that includes a having clause? For example: SELECT State.Name, Count(*) FROM State INNER JOIN StateOwner ON State.StateID = StateOwner.StateID GROUP BY State.StateID HAVING Count(*) > 1 ...

Problem with Generic Linq OrderBy function

I saw the following function in a posting which allows one to order data using a generic expression: public static IOrderedQueryable<T> OrderBy<T, TKey>( this IQueryable<T> source, Expression<Func<T, TKey>> func, bool isDescending) { return isDescending ? source.OrderByDescending(func) : source.OrderBy(func); } When I try to use t...

Need help with LINQ query that joins two tables containing periodic events

My real-life example is too obscure to explain, but this is a pretty good approximation of what I'm trying to do... Month table has columns: Id, Name Holiday table has columns: Id, MonthId, DayOfMonth, Name Appointment table has columns: Id, MonthId, DayOfMonth, Description How do I produce a list of unique events (holidays and appoi...

Can I cite a Compiled Query as the Data Source to a Compiled Query?

Best to describe this in code... I have this public class A<T> { public static class Queries { public static Func<DataContext, int, T>Get = CompiledQuery.Compile<DataContext, int, T>( (DataContextdb, int i) => (from t in db.GetTable<T>() ...

How do you sort a parent and child collection using Linq?

Hi all, I have the following basic classes (cut down for this question): public class Parent { public string Name { get; set; } public IList<Child> Children { get; set; } } public class Child { public string Name { get; set; } } If I have a Parent collection, what I'd like to do is get an IList that is sorted by Parent.N...

Pass variable type to a method without enumerating

I want to be able to pass a variable type to a method, mainly so that I can pass an entity framework query to a method that will apply common includes of nested object. This is what I want to do... public Person GetPersonByID(int personID) { var query = from Perspn p in Context.Persons where p.PersonID = personID ...

How to intercept and modify SQL query in Linq to SQL

Hi, I was wondering if there is any way to intercept and modify the sql generated from linq to Sql before the query is sent off? Basically, we have a record security layer, that given a query like 'select * from records' it will modify the query to be something like 'select * from records WHERE [somesecurityfilter]' I am trying to fi...

Generate Expression<> for filtering by a arbitrary property

I want to write filtering controls which take object type T and property name and return Expression<Func<T, bool>> that checks value of passed property. I don't want to use reflection because I'm afraid such expressions can't be used by EF. I can't use delegates because C# doesn't have delegates for properties. What can I do? Maybe I sho...

Join DataTable with List<SomeObject>

I have a DataTable and a List of objects. I need to return all rows in the DataTable where a property in the List is a certain value. The List is only used for filtering the datatable (but the filter column isn't containined in the datatable. I'm sure this must be possible with link The DataTable contains: MembershipID Username Pas...

Error 11 Error 2002: The EntityContainer 'SomeEntitieContainerName' for the conceptual model specified as part of this MSL does not exist in MetadataWorkspace.

I'm getting this error when I try to compile my webapplication. I have only an *.edmx named BancorlineDB.edmx that has a "Entity Container Name" setted to "BancorLineEntidades". ...

Linq expression to set all values of an array to a given value

I have a bit of code that i'd like to turn into a linq expression (preferably with lambdas) to make it easier to use as a delegate. The code looks like this: List<DateTime[]> changes = new List<DateTime[]>(); changes = PopulateChanges(); for (int i = 0; i < changes.Count; i++) { for(int j = 0; j < changes[i].Length; j++) { ...

SqlException (0x80131904): Line 28: Incorrect syntax near '('.]

Please someone help me! I have an app running on windows 2003 with sql server 2005. When I try to deploy this some app in other server on windows 2003 with sql server 2000, some pages of the app shows the message bellow: Server Error in '/' Application. Line 1: Incorrect syntax near '('. Description: An unhandled exception occurred dur...