linq

Using LINQ to get the results from another LINQ collection

I have a LINQ statement which pulls the top N record IDs from a collection and then another query which pulls all records which have those IDs. It feels very clunky and inefficient and i was wondering if there might be a more succinct, LINQy way to get the same results var records = cache.Select(rec => rec.Id).Distinct().Take(n); var r...

collation conflict

Is there any one who knows how we can solve collation issue in select linq query? I'm getting this error when I want to select data in linq. Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operation var lstData = from s in dataTrackDB.datas join b in...

Linq to xml and counters across multiple projections

Similar to this question: http://stackoverflow.com/questions/372945/linq-iterators-selecting-and-projection I'm wanting to use a counter or incrementing variable in a projection - however I'm wanting to use the same counter across multiple projections. I'm constructing an xml document using a number of projections based on different da...

Find the distance required to navigate a list of points using linq

Using linq given a IEnumerable of two dimensional points, Find the distance required to navigate all points assuming the points are visited in the order they appear in the list. I thought I might be able to somehow use the .Aggregate<> function but can't figure out how to make it work. The problem is the aggregate returns the same type...

Linq to Entities: adding a where condition to a child relationship

For example I have a list of customers which each have a list of orders. Now, I want to get a list of all customers with unpaid orders (let's say this is status 2). Together with that list of customers, I want to have the list of unpaid orders also. For example I have this: from c in mycontext.Customers.Include("Orders") select c Whe...

Autocomplete Extender not sorting alphabetically

Hello everyone. I am using LINQ within a webservice that creates an autocomplete function on a text box. I've got it to work but unfortunately the results are not being populated in the order I expect, for example, if I was search for drinks beginning with "carl" I'd expected "carling" first and then "carlsberg" but this is not the case...

How to convert an anonymously typed List to List<T>?

This simple Linq query: from c in mycontext.Customers join o in mycontext.Orders on c.CustomerId equals o.CustomerId where o.Status == 1 select new {c, o} will result in List<{c:Customer, o:Order}> after calling ToList(). What's the easiest way of converting this anonymously typed list into a list of customers (List<Customer>)? E...

NHibernate.Linq - Custom/Calculated property expression

How can a domain object include a property that calculates a value from other database mapped properties so that the calculated property can be used in both the domain object instance and the db level by nhibernate.linq. I would like to be able to use the property when working with the object directly: Console.WriteLine(Entity.Calculat...

Most efficient way of loading data into LINQ object for search result type method

I currently have the following: public IEnumerable<News> NewsItems { get { return from s in News.All() where s.Description.Contains(SearchCriteria) || s.Summary.Contains(SearchCriteria) select s; } } The problem is I only need to return the one property that actually has the data as well as the Title proper...

How can I hierarchically group data using LINQ?

I have some data that has various attributes and I want to hierarchically group that data. For example: public class Data { public string A { get; set; } public string B { get; set; } public string C { get; set; } } I would want this grouped as: A1 - B1 - C1 - C2 - C3 - ... - B2 - ... A2 - B1 - ......

Linq to SQL writing translatable functions

I'm pretty sure the answer to this is "you can't do that" or "No, no, you misunderstand...", but: I have a linq class, thing, which will happily execute this: var thingsWithAandB = from t in db.things where t.propA.HasValue && t.propB.HasValue select t; But I do this a lot, and so I wan...

SQL query to return columns and values which match part of a where condition

Hi all, I am trying to find out if there's a good way to get all the column names, and values for a particular row, where a part of a condition is met. That is, I want to know which fields within my huge nested AND and OR where condition, met which conditions, and their values. The catch is I am actually using the Dynamic LINQ API over...

append to an expression - c#

I followed this thread: link text Jason gives an example: public static Expression<TDelegate> AndAlso<TDelegate>(this Expression<TDelegate> left, Expression<TDelegate> right) { return Expression.Lambda<TDelegate>(Expression.AndAlso(left, right), left.Parameters); } and its usage as such: Expression<Func<Client, bool>> clientW...

C# - Entity Framework - Doing a reverse CONTAINS?

I am using VS2008 (no option for VS2010 right now) and am doing a (reverse) Contains to try to match a partial string to a whole string. For instance, the urlRef = http://www2.rivworks.com/feed-test-1/?SSScrollPosition=0 and the defaultUrlDomain = www2.rivworks.com. IList<vwCompanyDetails> efMatchingUrlCompanyList = null; ... efMatchin...

SQL Query to Linq GroupBy and Max

Here is a SQL query that I am trying to convert in to Linq. I am using a generic list of objects and not a DataTable if that is relevant. Select Max(Date), ID, Property1, Peroperty2 From List Group By ID Please help. ...

Can I refactor a range variable in a linq statement?

I have an entity framework 3.5 project where I'm using TPH inheritence. Two of my concrete types are in their own DAO class and contain a selector that projects the Entity Class into a DTO. However, both of these concrete classes have similar relations with another table which I use a let statement to identity clearly. My question is,...

NHibernate linq - Use lambda expression in place of formula attribute on mapping.

NHibernate has an attribute on the property element in the mapping config named "formula" that allows the injections of sql to "calculate" a property. The issue I have is the formula using sql syntax directly. Is there a way to have nhibernate.linq to use a lambda expression instead of using the formula property. I have the following: ...

LINQ to SQL doesn't update if I include a calculated column

I am populating a DataGrid with the following LINQ code : Dim myClients = From p In dc.Persons _ Select p I can navigate my DataGrid, make changes and then click on a button that calls dc.SubmitChanges() All of this works well and updates SQL Server. I then wanted to add a single additional colu...

LINQ composition - generates a weird FROM clause

Possible Duplicate: Why did the following linq to sql query generate a subquery? I have a qs. for you LINQ gurus... I am using LINQ in a composable way and the SQL being generated is a bit complex and of the form: SELECT xxx FROM ( SELECT yyy from myTable1, myTable2 WHERE foo == bar ) AS t7 WHERE t7.column == value. ...

LINQ & Enums as IQueryable

I basically have an enum public enum WorkingDays { Monday, Tuesday, Wednesday, Thursday, Friday } and would like to do a comparison against an input, which happens to be a string //note lower case string input = "monday"; The best thing I could come up with was something like this WorkingDays day = (from d in Enum....