linq

troubles creating a List of doubles from a list of objects

Hi, i have a list with objects. The object has a property 'Sales' which is a string. Now i want to create a list of doubles with the values of all objects' 'Sales' properties. I tried this: var tmp = from n in e.Result select new{ Convert.ToDouble ( n.Sales) }; but this gives me this error: Error 106 Invalid anonymous type member d...

How does linq Last() work?

I dont understand how current can be null and last can be an object while last being a LINQ function. I thought last uses GetEnumerator and keeps going until current == null and returns the object. However as you can see the first GetEnumerator().Current is null and last somehow returns an object. How do linq Last() work? var.GetEnumer...

Calculate average in each group

I am using the following class class Country { int CountryID {get; set;} List<City> city {get; set;} } class City { int CountryID {get; set; } string city {get; set;} int sqkm {get; set;} } Here's is some sample data for Country and City Country US UK Canada City CityC CityF CityA CityB CityG CityD CityE I a...

how to specify literal value in linq union

I have the following SQL query, note I want the literal value '0' in the second field in the second SELECT statement from the ItemSale table. How do I express this in LINQ? I get the error message 'Invalid anonymous type member declarator'. SELECT BranchNumber,QuantitySold FROM Department UNION SELECT BranchNumber,0 FROM ItemSale How...

Accessing child collection in query

I am populating a list using List<Country> countries = new List<Country> { new Country() { CountryID = "US", City = new List<City> { new City() { CountryID = "US", CityName = "dfdsf", sqkm = 2803 } } } }; and so on How to access sqkm in the following query? v...

Get top N records using LINQ to Entities

Hi all, I am using Linq to entities and would like to know if I can get a limited number of records when i query. I just need the top N records as the query do the orderby and other clauses. Is this possible or I will have to get the top N using foreach loop? Thanks in advance for Ideas and suggestions, Abdel Olakara ...

How do you create a nested LINQ Grouping / Lookups for a Collection of Objects

Given a list of objects with a date and decimal, I would like to index these objects by the year, month, dayofweek and hour of the date. In .NET 2.0, I would have created this graph using a set of nested dictionaries and a list as the leaf node. I am interested in how this could be done with LINQ. ...

LINQ - Multiple Where based on conditions.

I want to query a table with some conditions based on user input. I have this code: IQueryable<Turno> turnoQuery = dc.Turno; if (helper.FechaUltimaCitaDesde != DateTime.MinValue) { turnoQuery = turnoQuery.Where(t => t.TurnoFecha >= helper.FechaUltimaCitaDesde); } if (helper.FechaUltimaCitaHasta != DateTime.M...

LINQ, Left Join, Only Get where null in join table

Hi. I am trying to do a left outer join on two tables, but I only want to return the results from the first table where the second table does not have a record (null). var agencies = from a in agencyList join aa in joinTable on a.AgencyId equals aa.AgencyId into joined ...

Returning IEnumerable<T> vs IQueryable<T>

what is the difference between returning iqueryable vs ienumerable. IQueryable<Customer> custs = from c in db.Customers where c.City == "<City>" select c; IEnumerable<Customer> custs = from c in db.Customers where c.City == "<City>" select c; Will both be deferred execution? When should one be preferred over the other? ...

C# LINQ: Join and Group

I have two tables TableA aId aValue TableB bId aId bValue I want to join these two tables via aId, and from there, group them by bValue var result = from a in db.TableA join b in db.TableB on a.aId equals b.aId group b by b.bValue into x select new {x}; My code doesn't recognize the join after the group. In other words, the grou...

Using LINQ-To-Entities to Generate Information

I am working on a website where a user can add tags to their posted books, much like is currently done for questions on Stack Overflow. Classes: Books { bookId, Title } Tags { Id Tag } BooksTags { Id BookId TagId } Here are few sample records. Books BookId Title 113421 A 113422 B Tags Id Tag 1 ASP 2 C# 3 CSS 4 VB 5 V...

select multiple columns using linq and sum them up

Hi, How can i select multiple columns and calculate the total amount. For example, in my database i got a few fields which are named: 5hunderedBills, 2hunderedBills, 1hunderedBills, etc. And the value of those fields are for example: 5, 2, 3 And the sum would be: 5hunderedBills * 5 + 2hunderedBills * 2 + 1hunderedBills * 3 How ca...

Executing LINQ Queries In Excel To SQL

I want to be able to interact with an SQL database using LINQ and Excel (it's a legacy thing). Is this sort of thing possible? I'm screwing around with SQLMetal, and have generated some database classes in VB, but they don't seem to go in smoothly into Excel (I'm getting a lot of red error text). Any ideas would be appreciated. ...

Issue with DBNull when using LINQ to DataSet

I've got the following LINQ Statement: Dim PSNum As Integer = 66 Dim InvSeq = (From invRecord In InvSeqDataSet.RptInvSeqDT.AsQueryable() _ Where IIf(invRecord.IsPack_NumNull(), False, invRecord.Pack_Num = PSNum) _ Select New With _ {.Inv = invRecord.Invoice_Num, .Seq = invRecord.Inv_Seq})...

How can I create an empty collection from an anonymous collection most elegantly in C#?

If I have an anonymous type created by LINQ var ans = from r in someList where someCondition(r) select new { r.a, r.b }; What is the best way to create an empty matching collection so I can move elements to the new collection: var newans = ? foreach (r in ans) { if (complicated(r) newans.Add(r); } Is there some way to use Enumerab...

C# - Help with LINQ

I need to check if a certain property exists within a class. Please refer to the LINQ query in question. For the life of me I cannot make the compiler happy. class Program { static void Main(string[] args) { ModuleManager m = new ModuleManager(); IModule module = m.FindModuleForView(typeof(HomeView)); Con...

EF4. Add a object with relationship causes full table select

Ex 1: "autor.ComentariosWorkItens.Add(comentarioWorkItem);" autor.ComentariosWorkItens makes EF4 load all ComentariosWorkItens. Ex 2: comentarioWorkItem.Usuario = autor; Fixup make EF load all ComentariosWorkItens too: private void FixupUsuario(Usuario previousValue) { if (previousValue != null && previousValue.Come...

How to order the items in a nested LINQ-provided collection

I've got a (SQL Server) database table called Category. And another database table called SubCategory. SubCategory has a foreign key relationship to Category. Because of this, thanks to LINQ, each Cateogory has a property called SubCategories and LINQ is nice enough to return all the SubCategories associated with my Category when I grab...

Convert this Linq query from query syntax to lambda expression

I'm not sure I like linq query syntax...its just not my preference. But I don't know what this query would look like using lambda expressions, can someone help? from securityRoles in user.SecurityRoles from permissions in securityRoles.Permissions where permissions.SecurableEntity.Name == "Unit" && permissions.PermissionType.Name == "R...