linq

Linq Expressions and Aggregate classes

How do I build Linq Expressions for aggregate classes? Let me explain with an example of what I am trying to do. Say, I want to find all employees with pay not equal to 50000 and here's the class structure. Employee e1 = new Employee { Name = "Jane", PDetail = new PayDetail { Salary = 100000 } }; Employee e2 = new Employee { Nam...

NHibernate QueryOver<T> with Interfaces??

Hi, I have a repository object that knows the Type of the mapped hibernate class. But it doesn't have a generic <T> type but instead a reference to a Type object Type TheMappedType. So I cannot do something like: session.QueryOver<typeof(TheMappedType)>().... Usually what I can do with hibernate is: session.Get(typeof(TheMappedType), s...

Intersect lists on KeyValuePair key?

How do I insertsect two lists of KeyValuePairs based on their keys? I have tried: List<KeyValuePair<string, string>> listA = new List<KeyValuePair<string, string>>(); List<KeyValuePair<string, string>> listB = new List<KeyValuePair<string, string>>(); ... var result = listA.Intersect(listB); Which expectedly doesn't work. Do I need to...

Tough Linq Query

I have an IEnumerable of invoices, these invoices have line items. These line items have a priority. I'm programming a variety of strategies to automatically apply cash against these line items and one is giving me some trouble. My pattern has been to prepare a linq statement to order the line items of the invoices then iterate over the...

Using LINQ to XML to traverse an HTML table

So, I can easily use LINQ to XML to traverse a properly set-up XML document. But I'm having some issues figuring out how to apply it to an HTML table. Here is the setup: <table class='inner' width='100%'> <tr> <th> Area </th> <th> Date </th> <th> ID ...

Linq to NHibernate query issue

Why would the following work: var profiles = (eventId > 0) ? Profiles.Query().Where(p => p.Event.Id == eventId).ToList() : Profiles.Query().Where(p => p.Event == null).ToList(); when the following will not: var profiles = (from p in Profiles.Query() where (eventId > 0) ? p.Event.Id == event...

Help with Linq to XML

Hi, Iv got two DB tables. One containing types(Id, Name) and the other holds datapoints (RefId, Date, Value) that are referenced by the types. I need to create a XML file with the following strukture: <?xml version='1.0' encoding='utf-8' ?> <root> <type> <name></name> <data> <date></date> <temp></temp>...

LINQ extension methods - Any() vs. Where() vs. Exists()

Unfortunately the names of these methods make terrible search terms, and I've been unable to find a good resource that explains the difference between these methods--as in when to use each. Thanks. Edit: The sort of query that I'm trying to fully understand is something like this: context.Authors.Where(a => a.Books.Any(b => b.BookID ...

Using Linq and C#, how could I kinda merge two lists over some criteria?

The context public class Item { public int Index; public string Text; } ... var items = new List<Item> { new Item {Index=1, Text="Data #1"}, new Item {Index=8, Text="Data #8"}, new Item {Index=4, Text="Data #4"}, }; The code var data = (from item in items orderby item.Index select item)...

Simplest way to perform grouping/projection using Linq To Objects

I'm getting data from the data layer that I need to transform in the middle tier using Linq To Objects (I don't control that tier of the application). I have to perform a grouping operation using multiple keys followed by a string concatenation on the non key-fields. Given the data below, I want to group by CustomerID and Date, and cre...

Common Linq / Standard Query Operator mistakes/mis-steps?

For programmers that do not come from a functional programming background, are there an mistakes to avoid? ...

Need help trying to EagerLoad some data in EntityFramework.

Hi folks, I'm trying to do some eager loading on an EF Entity. so, if the entity is called Orders .. then I guess i would do the following... _someContext.Orders.Include("Whatever") .... But the problem is, I have a method like the following ... public IQueryable<Order> Find(Expression<Func<Order, bool>> predicate) { return Curr...

Complex Linq Query--Trying to add a .Where() or .Any() predicate to reduce query complexity (in VB)

I'm having issues with a rather complex Linq query and I need some advice. This involves VB syntax so I need specific answers for that platform, as I have a lot of trouble translating the C# syntax to VB at times. I have to join two main tables, and I need to filter the results by elements in an ASP.NET web form. These filters are c...

what is equivalent c# code for following statement?

var files = from file in my.Computer.FileSystem.GetFiles(CurDir()) orderby file select file; ...

LINQ to Entities many to many relationship

I am new to LINQ and the Entity Framework and am having trouble coming up with a suitable query. I have the following entities. I have included primary keys and some other relevant fields. Contact Int ContactId(PK), String Name, String EMailAddress Project Int ProjectId(PK) ProjectContact Int ProjectId(PK), Int ContactId(PK), Boolean...

System.NotSupportedException: Can only project the last entity type in the query being translated.

I'm using LINQ with an ODATA web service from tp in TyrePatterns from t in tp.Tyres where t.Diameter == 195 select tp Seems simple enough, right? Tyres is a propery of TyrePatterns. So just to be sure you can see what I want to do, what I'm doing in the magical world of SQL would look something like: SELECT DISTINCT TyrePatterns.Name...

Muliplying all values in IEnumerable<int>

I have the following code and I am trying to work out how to multiply all values in my IEnumerable. I thought there might by a Multiply method like there is with Sum. I guess I could do a foreach over each item but these days this seems tedious. Any suggestions? //1:2:6 string[] pkgratio = comboBox1.SelectedRow.Cells["PkgRatio"].Valu...

LINQ BuildContainsExpression With OR conditions

Hi, I'm trying to get the following SQL query to work in LINQ: Select id from table1 where id in (1,2) or canceledId in (1,2) I'm using BuildContainsExpression to achieve the "IN" condition, but I can't figure out how to implement the "or" condition. My shot in the dark is as follows: var identifiers = new List<int> {1,2}; va...

Is retrofitting Entity Framework into my app appropriate for my situation?

So, I have an app that uses a SQL Server express db. I have about 80ish tables all with a primary key but no foreign keys. (The reason we have no foreign keys is because of how we do our sql client-to-server replication. It's not true replication but it's a sync system that was in place when we took over the app. We have no guarantee wha...

Is possible to use generics in LINQ-to-SQL mapping?

Hi all, Is there a way to define the following structure in a DataContext/DBML file? public class Entity { public int Id { get; set; } public string Name { get; set; } public EntitySet<IPermission> Permissions { get; set; } } public class User : IPermissionHolder { public int Id { get; set; } public string Name { ge...