linq

Initializing strongly typed objects in LINQ to Entities

I have a plain old CLR object which is essentially a wrapper for two entity framework objects, I'm doing this so I can pass this wrapper object to a strongly typed view in the MVC framework. My foo wrapper class is very simple: public class FooWrapper { public FooWrapper(Foo f, Bar b) { this.FooObject = f; this.B...

JQGrid edit form dropdown populated from database

My edit form requires a dropdown to be populated by a database call. I realize I must set the dataurl parm in the colModel. Does anyone have an example using a linq to sql call which returns the data in the format the jqgrid is looking for. Thanks. ...

Is it just me? I find LINQ to XML to be sort of cumbersome, compared to XPath.

I am a C# programmer, so I don't get to take advantage of the cool XML syntax in VB. Dim itemList1 = From item In rss.<rss>.<channel>.<item> _ Where item.<description>.Value.Contains("LINQ") Or _ item.<title>.Value.Contains("LINQ") Using C#, I find XPath to be easier to think about, easier to code...

Overloading LINQ's Add method for validation

I have an Email object and am attempting to validate the number of attachments that are in its List<Attachment> property. The trick is that we consume the Send() method across a WCF service. It's easy to validate it server side, but I want to validate it client side first. I have generated a library that others are supposed to use in ...

Is LINQ generally slower than a equal SQL statement

If I write a large SQL statement with many group by clauses and so on; would it be much faster with normal SQL (maybe a stored procedure), or is Linq only parsing it to a very nice SQL statement and gives me my results quite fast? ...

How do you do Multiple Inner Joins in Linq to Entities

I have already searched through SO and could not fins a workable solution for this. I am just trying to figure what is the syntax for multiple inner joins in Linq to Entities. Thanks ...

Linq If Statement

How would i write something like this in linq to entities sb.Append(" WHERE question.question_isdeleted = 0"); if (catid != 0) sb.AppendFormat(" AND (CatID IN ({0}))", catsSTR); if(!string.IsNullOrEmpty(AuthorID)) sb.Append(" AND (question_ownerid = @id)"); i think I just need the syntax to write an if conditio...

LINQ select problem

from item in db.Items join ci in db.ChainStoreItems on item.ItemId equals ci.ItemId where ci.ChainStoreId == 2 select item The problem is as follows: Item has a Set of ChainStoreItems. I want to make a qurey which returns a Item which dont have a Set of ChainStoreItems it should hold only the one specific ChainStoreItem for the select...

Custom sorting with LINQ

It seems that i'm missing something trivial. Anyway, here it goes: var order = new[]{1,3,2}; var foos = new[]{new Foo{Id=1}, new Foo{Id=2}, new Foo{Id=3}}; How to sort foos by order array using Linq? Desired result: foos == new[]{new Foo{Id=1}, new Foo{Id=3}, new Foo{Id=2}}; Edit: Order contains Foo ids. Sorry that i didn't m...

Using C# Count() with a function

I'm trying to see how many times the maximum value of an array occurs within the array by using .Count() with a function inside. However I don't fully understand how to do it. From reading the MSDN's scant example I thought I understood, however apparently not! This is what I thought of: string[] test = { "1", "2", "3", "4", "4" }; str...

nhibernate query with child entities and lazy="false"

Hi All I have a problem with seperate sql queries being generated for every item in a child collection when selecting the Parent. I have a Parent object with an IList collection of Child objects. If I run the following code using linq to nhibernate: IList parents = _repository.ToList(); I get the sql statements like the following: S...

Null Reference Exception in a Dynamic LINQ Expression

I am using the Dynamic Linq Library / Sample from Microsoft to do ordering on a list. So for example I have the following C# code: myGrid.DataSource=repository.GetWidgetList() .OrderBy(sortField + " " + sortDirection).ToList(); I have a case where my Object have a 0:1 relationship with another object, which has a property ...

Linq query to exclude from a List when a property value of List of different type are equal?

I have a List of type Fee from which I need to exclude the ones that have an ID that exists in another List of type int. List<int> ExcludedFeeIDs = new List<int>{1,2,3,4}; List<Fee> MyFees = (from c in ctx.Fees select c).ToList(); Example: List GoodFees = (from f in ctx.Fees where f.FeeID!=One of the IDs in Exclu...

MVC - Linq - Populate List<T> with Records in Another Table

I'm prototyping my first MVC application, it's a simple forum. I've done part of the domain model and I'm trying to figure out how to do something that's pretty basic in SQL alone, but I can't figure it out in my application. Here are my Entities: [Table(Name="Users")] public class User { [Column(IsPrimaryKey=true, IsDbGenerated=t...

Linq equivalent of TSQL query

What would be the Linq equivalent of the following TSQL query: SELECT c.[CustomerId] ,c.[Name] , (SELECT COUNT(*) FROM Incidents WHERE CustomerId = c.CustomerId) AS IncidentsCount , (SELECT COUNT(*) FROM Opportunities WHERE CustomerId = c.CustomerId) AS OpportunitiesCount , (SELECT COUNT(*) FROM Visits WHERE CustomerId = c.Custo...

Enumerating through an object's properties (string) in C#

Let's say I have many objects and they have many string properties. Is there a programatic way to go through them and output the propertyname and its value or does it have to be hard coded? Is there maybe a LINQ way to query an object's properties of type 'string' and to output them? Do you have to hard code the property names you w...

LINQ Between Operator

The following works fine with IEnumerable types, but is there any way to get something like this working with IQueryable types against a sql database? class Program { static void Main(string[] args) { var items = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, }; foreach (var item in items.Where(i => i.Between(2, 6))) ...

Are there any production-ready LINQ implementations for javascript?

Background: Just for laughs I decided to search to see if anyone out there had developed a JavaScript implementation of LINQ. Lo and behold, there seem to be several of them. It would seem like LINQ implementations are almost like string Template processing engines: everyone and his dog has written one. Question: Does anyone out there...

Subsonic query with critera on a one-to-many relation

When using SubSonic 3.0 i have encountered a small issue that may be quite easy to solved, but please, enlight me with the solution :) I have a NewsArticle table with some news, each newsarticle can be published on different sites. So there is a Site table and also a NewsArticleSite table. The NewsArticleSite table contains a primary ke...

How do I use a 'foreign key' as criteria in a LINQ to Entities query?

I have a Departments table, which has a foreign key column to Sites, on Department.SiteId = Sites.SiteId. Now in my EF model, my Departments entity doesn't have a Siteid attribute, just a reference to Sites. How do I select, in a LINQ query, all departments with a specific SiteId? ...