linq

LINQ to SQL: GroupBy() and Max() to get the object with latest date

Consider a SQL Server table that's used to store events for auditing. The need is to get only that latest entry for each CustID. We want to get the entire object/row. I am assuming that a GroupBy() will be needed in the query. Here's the query so far: var custsLastAccess = db.CustAccesses .Where(c.AccessReason.Leng...

Subsonic Linq guid problem

The construtor 'Void .ctor(System.Guid, Int32)' is not supported. this error occured with the following statements: var Test = from r in db.UserRoles join p in db.UserPermissions on new { r.userId, r.roleId} equals new { p.userId, p.roleId } select r; userId is a guid roleId is an integer ...

Processing large datasets using LINQ

Every time I write a program of the form below using LINQ to SQL, I end up with a program that just grabs more and more memory as it runs and falls over in a heap consuming 2GB after perhaps as little as 25,000 records. I always end up rewriting it using ADO.NET. What am I doing wrong? Clarification: This question is not about speed ...

When selecting an anonymous type with LINQ from EF, is there no way to run a method on an object as you select it?

Let's say I have a method: bool myMethod(int a) { //return a bool } So let's say I the following // assume a has prop1 and prop2 both ints var mySelection = from a in myContainer where a=somecondition select new { a.prop1, myMethod(a.prop2) ...

linq sql where closest to number

i have a table Id Number 1 9 2 10 3 12 4 19 5 20 select Id where Number is closest to 18 it should return row 4 which is 19 how do i write this in linq and tsql? thanks ...

Threading / Linq Class list problem

Ok, so ive been writing a very complex multiserver irc bot recently, and ive encountered an issue.. i stipped down the code as much as i could because its very large, the full code is here: http://pastie.org/691449.txt so what my issue is, when i call the Disconnect() void in Connection, instead of disconnecting and closing the given se...

Help with SQL/LINQ Debugging

I'm having trouble with the following statement, which is returning the error "Sequence contains no elements": var vUser = (from u in this.dcLAUNCHOnline.aspnet_Users where u.UserName.Equals(this.wCreateUser.UserName) select u).Single(); The SQL being generated: SELECT [t0].[A...

How can I sort by a column that is included in a new{} section of a LINQ query?

The following uses the Northwind database and lists out product groups with four columns: lowest, highest, average, and total price. How can I sort by total price? var products = from product in Products group product by product.Supplier into groupedProducts //orderby groupedProducts.Key.TotalPrice; select new { groupedProducts.Key...

Using "cached" LINQ entities

Hi, i've got the problem that i implemented a WCF-service which is using LINQ entities... To generate the content of the response to a service call, requires a high amount of LINQ/SQL selects... Whatever, the service is not doing any updates or inserts to the DB, so i'd like to implement a kind of "caching" of the LINQ entities ( also ...

If IQueryable contains in model?

Hello I have this loop that renders a schedule with checkboxes for each entry like this: Monday Tuesday..... 08:00-09:00 [ ] [ ]... 10:00-11:00 [ ] [ ]... Using this code in my view: <% for (int i = 1; i < 10; i++) {%> <tr> <td> xx:00 - xx:00 </td> <% for (int current...

How to change a global variable value based on linq query while executing

I have two module-level variables and Linq query. I want the results of the Let clause to change the global variables - is this possible? For example: Dim X As Integer = 0 Dim Y As Integer = 0 Sub One() Dim query = From e In <picture> _ Let X = e.@x _ Let Y = e.@y _ Select <im...

Multi-dimensional arrays in Linq

Hey there! I have an XML file similar to the following: <novel> <paragraphs> <paragraph> <choice>This is paragraph 1</choice> <choice>Paragraph 1 alternative text</choice> </paragraph> <paragraph> <choice>This is paragraph 2</choice> <choice>Paragraph 2 alternative text</choice> ...

Get max + min in one line with linq

Hello How can I change this method so it also returns Max(t.StartTime) and Min(t.StartTime) with only using it in one line as below? public IQueryable<Timetable> GetTimetables() { return from t in _entities.Timetables select t; } /M ...

Get LINQ to preload a complete table

I need LINQ to grab a whole table, but this seems not to be working... everytime i select on the values via the pkey, a select is fired again.. So, actually this code: DataContext dc = new DataContext(); dc.Stores.ToList(); Store st = dc.Stores.SingleOrDefault(p => p.Id == 124671); is making a select * fro...

Parse string to C# lambda Func

Is there a way to convert string representation of lambda to a lambda Func? Func<Product, bool> func = Parse<Product, bool>("product => product.Name.Length > 0"); I tried Dynamic LINQ but it doesn't work as expected - for example it doesn't expect lambda syntax =>. Summary of answers: writing my own C# compiler - very funny firing ...

LINQ to append to a StringBuilder from a String[]

I've got a String array that I'm wanting to add to a string builder by way of LINQ. What I'm basically trying to say is "For each item in this array, append a line to this StringBuilder". I can do this quite easily using a foreach loop however the following code doesn't seem to do anything. What am I missing? stringArray.Select(x => s...

Linq return bool where a = val1 & b = val2 from model?

Hello I have 2 for loops and need to check if the model contains value based on current ones. <% for (int currentDay = 1; currentDay <= 7; currentDay++) { %> <%=Html.CheckBox("TimeRange" + currentDay.ToString())%> <%} %> Somehow I need to make the checkbox checked if the model contains data based on 2 parameters (...

LINQ - join , group by (multiple fields), group by, select new some fields

Hi, i need select some fields from a table in the query using join, but in the select new statement i dont have acess from the fields of the join table. var query = from p in persistencia.RequisicaoCompraItems join s in persistencia.Suprimentos on p.SuprimentoID equals s.SuprimentoID (i need get fields from this join) group p by ne...

Lambda Func delegate using interface

I have a class implementing an interface, and I need to return a Queryable<> list of that interface, given a certain Where predicate : public interface ISomeInterface { int ID{get;} IQueryable<ISomeInterface> GetWhere(Func<ISomeInterface,bool> predicate); } public class SomeClass : ISomeInterface { public static IQueryable...

C# Instantiate Generic List with Reflected Type Information

I am trying to do the following: Given TypeInfo after reflecting on a LINQ-to-SQL object with various EntitySet<> child collections, retrieve the collection Do some operations on the collection The code below does not compile, obviously - just looking for another way to do this [Note, "Facade" is the L2S object in question). The thi...