linq

Using LINQ to merge a list of objects

I have two lists of objects, using Linq I would like to merge them, but where the two lists contain objects with the same key, I only want the one with the greatest LastUpdated Value. I thought that I could somehow get a list grouping by key a with max(LastUpdated) then join back to the list joining on key and LastUpdated, but there mus...

Why does XmlReader skip every other element if there is no whitespace separator?

I'm seeing strange behavior when I try to parse XML using the LINQ XmlReader class. Test case below: it looks like whether I use (XElement)XNode.ReadFrom(xmlReader) or one of the Read() methods on XmlReader, it misses the second bar elements in the input XML. If any whitespace is added between the </bar> and <bar> then it will parse th...

Struggling to translate SQL Query to Linq in a Domain Services Class and Entity Data Model

Visual Studio 2008 | Silverlight 3 | SQL Server 2005 | Domain Services Class | Entity Data Model I have a database table "Students" with columns FirstName, LastName, Semester, ProgramOfStudy, Column etc....... The goal is to return a Total (number of students grouped by first letter of lastname), based on the first letter of the lastna...

Querying a SQL View, with Linq in a Domain Services Class and Entity Data Model

Visual Studio 2008 | Silverlight 3 | SQL Server 2005 | Domain Services Class | Entity Data Model I have a database table "Students" with columns FirstName, LastName, Semester, ProgramOfStudy, Column etc....... The goal is to return a Total (number of students grouped by first letter of lastname), based on the first letter of the lastna...

How to group by on a reference type property in linq?

Hi All, I have a class like this: public class Order { public int Id; public Person SalesPerson; ... } public class Person { public int Id; public string Name; ... } I writed a query in LINQ like this: Order[] orders = GetAllOrders(); var myResult = select o from orders group o by o.SalesPerson.Id into oGr...

LINQ string compare in VB when using NHibernate

Hi Come across an issue when doing a string compare as part of the where section of a linq expression against LINQ for NHibernate. from x in NhibernateObject where x.StringCol = "value" select x When it runs it retrns a runtime error about casting to an integer. I found a nice post about the issue and the solution at http://jason.pe...

Linq to objects error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

I am getting the following error when trying to return a list of new objects from a linq query. I am looking to return a stripped down entity for use in a selectbox and only need and id and name. Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index myViewModel.Regions = _regionRep...

Filtering a list of processes based on a list of string objects

Hi, I have a List of string objects which represent process names. I want to filter a collection of running Processes (retrieved by using GetProcesses()), by using the list of string objects I have. So if I want to look for the Sql Server process running in the processes collection, I will look for the string name as stored in the strin...

Location of XElement when querying over IEnumerable using LINQ

I have a linq query that is querying over IEnumberable. When I have a matching element for my where clause I would like to know the position of the element in the IEnumberable. var result = from e in elements where (string) e.Attribute("class") == "something" select e.Position(); The e.Position() of course do...

How to reflect types that have an interface of generic, and get that type of generic

My 1st objective is to filter the types based on a specific interface with a generic. My 2nd objective is to obtain the type of the generic parameter itself. public UserService : IUserService, IDisposable, IExportableAs<IUserService> { ... } I cannot assume the structure of the class, its interfaces (if any at all) or alike. The...

Where Predicates in LINQ

How can I specify conditions in Where predicates in LINQ without getting null reference exceptions. For instance, if q is an IQueryable how can I do like: Expression<Func<ProductEntity,bool>> predicate = p => !search.CategoryId.HasValue || (search.CategoryId.HasValue && search.CategoryId == p.CategoryId); var q2 = q.Where(predicate); ...

Strange problem with LINQ to NHibernate and string comparison

I'm using LINQ to NHibernate and encountered a strange problem while comparing strings. Following code works fine but when I un-comment: //MyCompareFunc(dl.DamageNumber, damageNumberSearch) && and comment: dl.DamageNumber.Contains(damageNumberSearch) && then it breaks down and seems that MyCompareFunc() always return true while dl.Damage...

Syntax for Join extension method

Hi, I have the following c# code. IQueryable<Invoice> query = service.GetInvoices(); query = query.Where(inv => inv.ProjectID == projectid); query = query.Skip(pageIndex * pageSize).Take(pageSize); I want to add a Join to a "Status" table using the Join extension method. Can't find many examples of the syntax for this. C...

Is it necessary not to use methods in LINQ queries?

Hi all, In following code is any difference when I put "dl.DamageCount > 5" directly in the query or when I move "dl.DamageCount > 5" to a method or function then just call it from query? It seems when I move it into a method, query does not work properly. Indeed it seems the function/method always return true regardless of condition e...

Why ordinary laws in evaluting boolean expression does not fit into LINQ?

In such a code: if (insuranceNumberSearch == null ? true : ei.InsuranceNumber.Contains(insuranceNumberSearch.Trim())) doSomething(); where insuranceNumberSearch is null, remaining expression is not null while in following code: var q = from ei in session.Linq<EmployeeInsurance>() where insuranceNumberSearch =...

how can i convert a list of objects to csv

if i have a list of objects called "Car" public class Car { public string Name; public int Year; public string Model; } and i have a list of List and i want to generate a csv file dynamically from this list ...

LINQ JOIN + GROUP BY + SUM

Hey there, I have two LINQ statements that I would like to make into one, but for the life of me I can't get it to work. I can't get the grouping to work in the first statement. It complains that the TotalBuy and TotalSell properties aren't there, although doesn't complain about AmountTC and AmountAUD. This should be simple. Any thou...

asp.net mvc select question

i have model, in model such method public Pages GetPage(int? id) { return _dataContext.Pages.First(p => p.id == id); } if i pass whong parameter (like 123333-no record it database), it throw exception Sequence contains no elements can you suggest correct code version, or simply use try\catch? ...

Short-circuit evaluation and LINQ-to-NHibernate

It seems that LINQ-to-NHibernate and LINQ-to-SQL does not support short-circuit evaluation in where clause of query. Am I right? Is there any workaround? May it be added to next versions of LINQ-to-NHibernate and LINQ-to-SQL? for more information plz see followings: http://stackoverflow.com/questions/772261/the-or-operator-in-linq-w...

WPF - new idea of develop asp.net app?

hello, I am a student and in last semester i develop asp.net application - "Payment Helper for School" . In this new semester i will have subject "Graduation Project" and i thinking about develop my application. In my old app i use: ASP.NET web forms mssql 2008 database linq to query to db I achieved technologies above at good level ...