linq

How to combine two expressions: result = exp1(exp2);

As subject, how to combine two expressions into a single one for this case: Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp1; Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp2; Expression<Func<IEnumerable<T>, IEnumerable<T>>> result = ???; // exp1(exp2) ...

C# Reading a File Line By Line

I am trying to read some text files, where each line needs to be processed. At the moment I am just using a StreamReader, and then reading each line individually. I am wondering whether there is a more efficient way (in terms of LoC and readability) to do this using LINQ without compromising operational efficiency. The examples I have s...

LINQ distinct join query

I am trying to get a distinct list of tags for a record. It sort of works except the tag name is always the first one repeated. public EntityCollection<TagEntity> Tags { get { EntityCollection<TagEntity> x = new EntityCollection<TagEntity>() { new TagEntity(){Id=1, IsActive=tru...

Programmatically getting executable SQL from a LINQ query at run-time

I know about Scott Gu's LINQ to SQL visualiser, but is there a way to take a LINQ expression at run-time and do something to find out the full resulting SQL? ...

databind a sharepoint list to a dropdown using linq

I have access to a sharepoint list like so: SPList countries = site.RootWeb.Lists["Countries"]; the list has a CountryCode column and a CountryName column using linq how can i databind this as a datasource for a drop down so that the "Value" is "CountryCode" and the "Text" is "CountryName" ...

Entity Framework creating IQuerable of the most recent

Hi I have an Entity set that has Entities with a compound key containing ID (GUID) and CreatedAt (DateTime). This CreatedAt is when the entity was created. Each record represents each version of each entity such that muliple records can have the same ID. I want to create an IQuerable-returning method that I can re-use such that it will...

ASP.NET MVC Model Binding Related Entities on Same Page

This problem has been driving me crazy for several hours now... In my domain, I have 2 entities that are related to each other Sku and Item. Each sku can have many items. public class Sku { private readonly EntitySet<Item> items; public Sku() { items = new EntitySet<Item>(AttachItems, DetachItems); } public...

Is there a better way to do this LINQ statement block?

I'm relatively new with LINQ, but I'm going to be getting into it a lot more. Is the following a practical application of LINQ, or is there a better way to do something like this? Public Shared Function GetItems(ByVal itemsList As List(Of OrderItem), ByVal whichForm As ItemsFor, ByVal formID As Integer) As List(Of OrderItem) Dim item...

There has to be a better way to add this clause in linq

var result = from R in db.Clients.Where(clientWhere) join RA in db.ClientAgencies on R.SysID equals RA.SysID join A in db.Agencies.Where(agencyWhere) on RA.AgencyID equals A.AgencyID join AC in db.AdCommittees on A.AgencyID equals AC.AgencyID into temp from x in temp.DefaultIfEmpty(...

LINQ to SQL - Group By

I'm trying to figure out how to create the LINQ-to-SQL for the following SQL and not having any luck. Any help would be appreciated. C# code for the response is prefered. Also, indexes other than PKey not shown for brevity. Table Schema: CREATE TABLE [dbo].[FileHashes]( [ID] [uniqueidentifier] NOT NULL, [FileName] [nvarchar](255) NU...

Advanced linq book?

What is the best advanced LINQ book? I'd like a book that covers in depths the inner workings of LINQ including lambdas, expression trees, etc... ...

efficient query for many to many table

Hi all, I'm writing a unit test of my mock-up repositoy. While the real repository lays on the linq2sql, the mock-up only use list of linq2sql objects. When it comes to the many to many tables, the efficency is rather low. Here is the senario. I have a many2many table which is tablePersonInCommunity, stores a CommunityID and a PersonI...

LINQ Query to retrieve multiple levels of relational data

I'm just getting up to speed with asp.net mvc and I'm wondering how one would go about getting relational data more than one level deep from the entity specified in the from clause. Using the following domain model as an example: A Blog has many posts. Posts have many comments. How would I write a LINQ query to return entities down to ...

C# Expression using And Or and Not expression together based on AST

I want to use Linq expression for some dynamic features. I need And, Or and Not expressions.. I couldn't get much.. We want to check whether certain feature has been enabled or not in our system and based on that we will decide whether to show the menu item or not. we have formed rules in XML format, I know to convert the rule to AST bu...

nhibernate table pr hierarchy fetching specific class with LINQ

I have a class hierarchy mapped into one table. There is one superclass and 8 different sub classes. A lot of my queries needs to fetch e.g. 2 of the sub classes only for a specific date. The table has a discriminator column that nhibernate itself uses. But when using LINQ for querying it is not possible to use this discriminator as ther...

Mapping POCO to string in Linq to Sql so that I can use Linq on my POCO

I'm accessing a database column where a phone number is stored as a varchar in the format +a-b:c where a = country code, b = phone number and c = extension. E.g. +44-07700123456:123 So I have a type that handles serialization along the lines of: public struct PhoneNumber { public PhoneNumber(string val) { /*...*/ } public override...

ASP.NET MVC, LINQ and ModelBinders...

Hi all, Is there a pre-built ModelBinder I can use with LINQ to get an object from a DataContext and update it on a HTTP post? For example, currently I have this block of code: [AcceptVerbs (HttpVerbs.Post)] public ActionResult Edit (Project project) { var projectService = Factory.GetService<IProjectService> (); project = proj...

How can I set LINQ SelectMany projection via Func parameter?

I have a function which returns a list of property values from a collection: public static List<string> GetSpeakerList() { var Videos = QueryVideos(HttpContext.Current); return Videos.Where(v => v.Type == "exampleType" .SelectMany(v => v.SpeakerName) .Distinct() .OrderBy(s => s) .ToList(); } I'd like to have a gener...

Help with LINQ to update an XDocument

Hello, I have some xml: <Response TaskId="2429"> <message>Run for cover.</message> <element location="proj\survival.cs"/> <element location="proj\run.cs"/> </Response> I would like to add an attribute to each item: <element location="proj\run.cs" status="running"/> Is that possible with LINQ in C#? Thanks any tips... ...

Add incremented property to IEnumerable by group using LINQ

To simplify the scenario, let's say we have a list of People with FirstName and LastName properties. Our data looks like this: Bob Smith Jane Smith Todd Grover Larry Lewis Jill Lewis Frank Lewis The first step would be to add an Integer property that gets incremented for each item: Bob Smith 1 Jane Smith 2 Todd G...