linq

LINQ DataTable Sum In Where Clause

I'm having a rough time figuring out exactly how to do a LINQ query on a DataTable and return a full row while having the WHERE clause test on a sum. My code: transactionsToRemove.AddRange(paymentTransactionResults .AsEnumerable() .Where(...) .GroupBy(r => (decimal)r["AccountNumber"])); There are multiple transactions ...

LINQ to Entities plus Set Theory: Unable to create a constant value of type 'ITextEntity'. Only primitive types ('such as Int32, String, and Guid')...

I have a LINQ to Entities query (using EF 4) that has some pretty complicated set-based filtering going on. The code compiles just fine, but when I try to run it, I get the following error: Unable to create a constant value of type 'ITextEntity'. Only primitive types ('such as Int32, String, and Guid') are supported in this context. ...

LINQ to SQL join 3 tables and select multiple columns and also using Sum

Hi, I have three tables Student, TimeSheet and TimeRecord. Talbe columns: Student : StudentId, FirstName, LastName TimeSheet: TimeSheetId,StudentId, IsActive TimeRecord: TimeRecordId,TimeSheetId, BonusHour(type int), CreationDate Table relationship: Student 1:N TimeSheet (FK StudentId) TimeSheet 1:N TimeRecord (FK TimeSheetId) ...

Need Func to supply to Where() method of both IEnumerable and IQueryable.

I have a Func defined as follows: Func<Foo, bool> IsSuperhero = x => x.WearsUnderpantsOutsideTrousers; I can query IEnumerables like this: IEnumerable<Foo> foos = GetAllMyFoos(); var superFoos = foos.Where(IsSuperhero); But when I try to supply the same Func to the Where method of an IQueryable, I get: 'Cannot convert source type ...

Select integers from string[], and return int[]

I have an array string[] with values that are mostly convertible to integers. var values = new [] {"", "1", "2", "a", "3"}; I need to convert values to an array of integers, discarding any items that aren't convertible. So I end up with var numbers = new [] {1, 2, 3}; What would be the most efficient (quickest and clean code) way ...

Get next N elements from enumerable.

Context: C# 3.0, .Net 3.5 Suppose I have a method that generates random numbers (forever): private static IEnumerable<int> RandomNumberGenerator() { while (true) yield return GenerateRandomNumber(0, 100); } I need to group those numbers in groups of 10, so I would like something like: foreach (IEnumerable<int> group in RandomNu...

Using the ALL operator in linq to filter child items of EntitySet

Hi, I have a two objects as follows: public class Item { public int ItemId {get;set;} public string ItemName {get;set;} public List<Tag> ItemTags {get;set;} public DateTime DateCreated {get;set;} } public class Tag { public int TagId {get;set;} public string TagName {get;set;} } These are LINQ-to-SQL objects,...

Repository pattern: One repository class for each entity?

Say you have the following entities defined in a LINQ class: Product Customer Category Should I have one repository class for all: StoreRepository ... or should I have: ProductRepository CustomerRepository CategoryRepository What are the pro & cons of each? In my case, I have several "applications" within my solution... the Stor...

LINQ-to-SQL - How to Include Property

I have the following example: using (MyContext context = new MyContext()) { var query = from entityA in context.EntityA.Include("TestProperty") join entityB in context.EntityB on entityA.Id equals entityB.EntityAId join entityC in context.EntityC on entityB.Id equals entityC.EntityBId ...

Avoid LINQ with Let keyword doing self-loop

I have three tables Student, TimeSheet and TimeRecord. Talbe columns: Student : StudentId, AssignedId, FirstName, LastName TimeSheet: TimeSheetId,StudentId, IsArchive, IsComplete TimeRecord: TimeRecordId,TimeSheetId, BonusHour(type int), CreationDate Table relationship: Student 1:N TimeSheet (FK StudentId) TimeSheet 1:N TimeRecord...

SelectList Extension - couple expressions

So I got this crazy idea I could make make something cool work. I got tired of new selectlist(item, "blah", "blahblah") so I started writing an extension method (trying to get it more strongly typed) something like this ... var selectList = projects.ToSelectList(p =>p.ProjectID, p =>p.ProjectName); the extension method goes a little ...

SubSonic 3 Linq Joining error after using CleanUp

I am using the ActiveRecord SubSonic3 templates. Everything was working just dandy, then I decided I wanted things to look a bit better. See my database has field that look like this: SomeKey_id SomeOtherKey_id I wanted to clean it up so that it would instead be SomeKeyID SomeOtherKeyID when accessing it through SubSonic. So I ad...

Linq to NHibernate does not return proper data

I have a class public class Item : IItem { public virtual Guid Id { get; set; } public virtual string Name { get; set; } public virtual bool IsActive { get; set; } } public interface IItem { Guid Id { get; set; } string Name { get; set; } bool IsActive { get; set; } } public class ItemMap : ClassMap<Item> { ...

Linq to XMl sorting method

Hi I have a datagridview which binds XML manually. I wish to sort out the columns by clicking column header. here is the method i wrote: when you click column header, it grabs column header and sorting data by that column header. I also put a toggle switch(direction) on that, so when user click again , the data can be sorted in differ...

LINQ to SQL sum null value

Hi, I have the following query, I'd like to sum the NULL value also. Some TimeSheet don't records in TimeRecord and some tr.TimeIn and tr.TimeOut are NULL. The query select only TimeSheet that has reords in TimeRecord. How I can have it select everything, and sum up the NULL value as well. So, the SUM of NULL will be just zero. Table ...

Can someone explain Query Reshaping in Linq with the following example?

I am reading this asp.net article on building your first asp.net mvc 2 website and I came across a Linq query that uses the Include method in the query. I have used some linq, but I have never used the Include method and would like a better explanation. Does it translate to an join in linq? What is the benefit? Here is the query from...

when and in what scenario to use Expression Tree

I was reading about Expression Tree feature and how you can create delegates using lambda expressions. I still can't get as to in what scenario it is useful and in what real world example should I use it. ...

Inlcude conditional checking in query with LINQ to SQL

Suggestion either in C# or VB.NET are welcome. Table relationship: Student 1:N TimeSheet (FK StudentId) TimeSheet 1:N TimeRecord (FK TimeSheetId) Dim query = From s In db.Students _ Let pair = (From ts In db.TimeSheets _ Join tr In db.TimeRecords On tr.TimeSheetId Equals ts.TimeSheetId _ Where ...

Design of LINQ code

What are your suggestions for designing linq code in project? Especially, I`m interesting in code design of big and complex linq queries? For example, you know, that you need to write a lot of huge linq stuff, maybe some of your code will have duplicate parts, maybe not, and you need: Make the code easily supportive - means, if you ne...

Return next 5 rows using LINQ in C# ASP.NET?

I'm using LINQ to query my database, and I don't want all the records returned all at once. Instead, user should be able to select Next 5 and Previous 5 using buttons. As of this moment I have selected the first 5 rows in the Item table in the databas like this: private void ShowItems() { var items = (from p in db.Items ...