linq

LINQ InvalidCastException error

Hi, I'm getting "InvalidCastException" (occurred in System.Data.Linq.dll) in my function: public User GetUserByKey(Guid key) { return usersTable.FirstOrDefault(m => m.UserKey == key); } which is called here: MembershipUser mu = Membership.CreateUser(user.UserName, user.Password, user.Email, null, null, true, Guid.NewGuid...

Aggregate in where clause of LINQ

I am trying to write a LINQ statement which selects only the rows in a table with the most recent rundate. For example, if I had a table with columns RunDate(datetime) and Value(decimal) one way of accomplishing this through sql might look like: SELECT Value FROM Table WHERE RunDate = (SELECT MAX(RunDate) FROM Table) Is there a way t...

How to I use Union in linq (vb.net)?

I know how to call the Union extension method, e.g. Dim r = productFirstChars.Union(customerFirstChars) However I do I do this with the Linq syntax, e.g. from productFirstChars select ???? ...

Does Array.ToArray<>() return the original array if it is the same type?

Hi! I deal with a framework on a daily basis where we sometimes provide methods that accept IEnumerable<MyBusinessObject> as a parameter in order to show user interfaces, perform calculations etc. If I pass in an array of MyBusinessObject like so: MyBusinessObject[] myArray = new MyBusinessObject { obj1, obj2, ..., objN }; frameworkCl...

c# Linq Object subQuery / In

Hello, I have 2 objects that contains generic list properties. IE : public class User { public string Sid { get; set; } public List<Group> Groups { get; set; } } public class Section { public string Sid { get; set; } public List<Group> Groups { get; set; } } From my BLL I get a generic list of sections List mySection...

is it possible to use linq with NHibernate just like with linq2sql or entity framework ?

I am using Fluent NHibernate and would like to use linq to query my database is this possible ? ...

Linq on 2 arrays in 1 loop?

Hi, Is it possible to improve the efficiency of those linq requests? I use two different loops... Can you help me to optimize this code? double[] x = { 2, 3, 1, 5, 7, 2, 3 }; double[] y = { 1, 2, 3, 4, 5, 6, 7 }; IEnumerable<int> range = Enumerable.Range(0, x.Length); double[] y_sorted = (fro...

Swap two items in List<T>

Is there a LINQ way to swap the position of two items inside a list<T>? ...

How to promote and/or sell LINQ to colleague?

How do you promote and/or sell LINQ syntax to colleague that doesn't see the benefit over the manual way of doing thing? For an example using Linq to Entity/Sql instead of using plain ado.net without any LINQ. ...

Truncate text in RadGrid column

Hi, using Telerik RadGrid* in a LINQ context, with ASP.NET/C#, how to truncate text to a maximum length when displaying in columns? By maximum, I mean if original string's length is shorter than the specified maximum length, no errors will raise. I have seen many examples of this on the net, but it seems that the Container.DataItem use...

How to convert IEnumerable<table> to xml or dataset

Hello. This is the code: IEnumerable<table> results = db_dane.ExecuteQuery<table>(query); table - is a database table, query - sql query (example: select * from table), db_dane - linq datacontext. How to convert results of this query to xml or dataset? ...

linq select items from child collection

Below is my calsses. I have a product that contains list of days. Each day has a city property. I need to create a linq query that will give me the distinct cities that are used on all my products in the system. I tried something like this but does not work; var cities = from product in NHibernateSession.Linq<Product>() select new { c...

Only parameterless constructors and initializers are supported in LINQ to Entities.

While trying a sem-complex query to display some ListView content on the page I got stuck on the famous "Only parameterless contstructor and initializers are supported in LINQ to Entities" error. Here is the code I used ... I can't find a place where I initialized something inside the query with parameters .... protected void ArtistsL...

Linq where column == (null reference) not the same as column == null

I came across a rather strange problem with linq-to-sql. In the following example, var survey = (from s in dbContext.crmc_Surveys where (s.crmc_Retail_Trade_Id == tradeId) && (s.State_.Equals(state)) select s).First(); If tradeId is null, it doesn't behave as if I h...

Log processing with LINQ

I know that it might not be the most performant, but I want to process some logs with a LINQ statement. Here is what the log looks like: RECORD DEVON 1 6748 bla bla bla bla bla bla bla bla bla bla bla bla RECORD JASON 1 7436 bla bla bla bla bla bla bla bla bla bla bla bla RECORD DEVON 2 9123 RECORD DEVON 3 3723 RE...

Linq query: does this array contain this string?

Is there a better way to do this? public bool IsServiceRunning(string serviceName) { string[] services = client.AllServices(); return (from s in services where s.Equals(serviceName, StringComparison.InvariantCultureIgnoreCase) select s).Count() > 0; } The case insensitivity ...

LINQ join with filter criteria

How is something like this done in linq? It has filter criteria on the JOIN. This is taken from this question: http://stackoverflow.com/questions/1401889/sql-filter-criteria-in-join-criteria-or-where-clause-which-is-more-efficient select salesman.salesmanid, max(sales.quantity) from salesman inner join sales on salesman.salesmanid =sa...

Linq, Left Join and Dates...

So my situation is that I have a linq-to-sql model that does not allow dates to be null in one of my tables. This is intended, because the database does not allow nulls in that field. My problem, is that when I try to write a Linq query with this model, I cannot do a left join with that table anymore because the date is not a 'nullable...

linq query where int ID belongs to List<int>

Hello, I'm having a problem with a linq query. I have used this similarly before but i cannot understand what could be wrong now. Errors The best overloaded method match for 'System.Collections.Generic.List.Contains(int)' has some invalid arguments Argument '1': cannot convert from 'int?' to 'int' ; refers to the where clause ...

LINQ expression works in LinqPad but not C# Silverlight Application

Hello, I have been working with a Linq query in a Silverlight application which returns only the row of a table which contains the max value of the field OptionARMRunId (identity). When executed in LinqPad, the query runs fine and returns the correct row. However, when used in my Silverlight application, the application never moves pas...