linq

LINQ: Count number of true booleans in multiple columns

I'm using LINQ to SQL to speed up delivery of a project, which it's really helping with. However I'm struggling with a few things I'm used to doing with manual SQL. I have a LINQ collection containing three columns, each containing a boolean value representing whether an e-mail, mobile or address is availble. I want to write a LINQ que...

How to select all parent objects into DataContext using single LINQ query ?

I am looking for an answer to a specific problem of fetching whole LINQ object hierarchy using single SELECT. At first I was trying to fill as much LINQ objects as possible using LoadOptions, but AFAIK this method allows only single table to be linked in one query using LoadWith. So I have invented a solution to forcibly set all parent ...

Select and select many difference

Hi, I have read many sites/threads on select and select many in LINQ but still don't quite understand. Does select return one element in a collection and select many flatten a collection (eg List>())? Thanks ...

Populate StackPanel with checkbox's from Database

I am quite new to WPF. I have a page that displays data from a SQL database using L2S. The L2S returns a DataTable that contains all the available options to choose from for a specific area. Every row it returns from the DataBase needs to be a checkbox and I want to put those checkboxes in a stackpanel. Am I looking at databinding to...

Ordering XElements

I have the following XML Document (that can be redesigned if necessary) that stores records and errors. <MYROOT> <RECORDS> <RECORD> <DATETIME>11/03/2010 14:12:41</DATETIME> <DOCUMENTID>1</DOCUMENTID> </RECORD> <RECORD> <DATETIME>11/03/2010 14:12:44</DATETIME> <DOCUMENTID>2</DOCUMENTID> </RECOR...

How to do Python's zip in C#?

Python's zip function does the following: a = [1, 2, 3] b = [6, 7, 8] zipped = zip(a, b) result [[1, 6], [2, 7], [3, 8]] ...

How best to calculate derived currency rate conversions using C#/LINQ?

class FxRate { string Base { get; set; } string Target { get; set; } double Rate { get; set; } } private IList<FxRate> rates = new List<FxRate> { new FxRate {Base = "EUR", Target = "USD", Rate = 1.3668}, new FxRate {Base = "GBP", Target = "USD", Rate = 1.5039}, new FxRate {Base = "USD", T...

is there a better way to write this frankenstein LINQ query that searches for values in a child table and orders them by relevance?

I have a table of Users and a one to many UserSkills table. I need to be able to search for users based on skills. This query takes a list of desired skills and searches for users who have those skills. I want to sort the users based on the number of desired skills they posses. So if a users only has 1 of 3 desired skills he will be furt...

What's faster? select [object] or select new {[object].value1, [object].value2} with linq

I was hoping some of you guru's out there know the answer to this question. Which is faster? A or B A: var item = from d in database select d; B: var item = from d in database select new {d.item1, d.item2}; SEEMINGLY, it seems like selecting part of the table object would be faster than selecting the entire o...

Trying to get all elements after first match using linq

How do I retrieve all elements after the first one not starting with a "-" using linq? var arr = new[] {"-s1", "-s2", "va", "-s3", "va2", "va3"}; var allElementsAfterVA = from a in arr where ???? select a; I want allElementsAfterVA to be "-s3", "va2", "va3" ...

return from a linq where statement

I have the following link function MyLinqToSQLTable.Where(x => x.objectID == paramObjectID).ToList(); I most of the time you can change a linq call to be several lines by adding curly brackets around the method body. Like this: MyLinqToSQLTable.Where(x => { x.objectID == paramObjectID; }).ToList(); Problem is the implied retu...

NHibernate Linq - Duplicate Records

I am having a problem with duplicate blog post coming back when i run the linq statement below. The issue that a blog post can have the same tag more then once and that's causing the problem. I know when you use criteria you can do the followingcriteria.SetResultTransformer(new DistinctRootEntityResultTransformer()); How can I do t...

Finding a sequence in a List

I have a list of integers that I would like to search for a sequence. For example, if i have a master list: 1, 2, 3, 4, 9, 2, 39, 482, 19283, 19, 23, 1, 29 And I want to find sequence: 1, 2, 3, 4 I would like some easy way to fill a subset list with: 1, 2, 3, 4 + the next five integers in the master list And then remove the integers ...

Linq method body Best Practice question

Which of these two statements is faster/better practice? myList.Where(x => { bool itemOne= x.ItemOne == paramItemOne; bool itemTwo = x.ItemTwo == paramItemTwo; return itemOne && itemTwo; }) myList.Where(x => x.ItemOne == paramItemOne).Where(x=>x.ItemTwo == paramIt...

LINQ: converting a query expression to use lambdas

I'm trying to rewrite a LINQ To Entities query to an expression. My model is a School which can have many Persons. Persons are inherited out to teachers, students, etc. The following query works for me: IQueryable<DAL.TEACHER> teacher = from p in School select p.PERSON as ESBDAL.TEACHER; How would I write this as a query expressio...

Sending items in a LINQ sequence to a method that returns void

Hello all. Often while I'm dealing with LINQ sequences, I want to send each item to a method returning void, avoiding a foreach loop. However, I haven't found an elegant way to do this. Today, I wrote the following code: private StreamWriter _sw; private void streamToFile(List<ErrorEntry> errors) { if (_sw == null...

Which ORM to use with SQL Azure?

Just wondering what everyones thoughts on what ORM to use for SQL Azure? I'm fairly comfortable using LINQ-to-SQL and I believe it is possible to get it working with SQL Azure. However, from my understanding (correct me if I'm wrong), no further improvements will be made to Linq-to-SQL in future releases of the .NET framework? Alter...

Translate Linq Expression to any existing Query structure?

I have some kind of "data engine" between multiple "data consumer" processes and multiple "data storage" sources. I'd like to provide Linq capabilities to the "data consumer" and forward the query to the "data storage". The forwarded query should be some structured query (like, let's say, NHibernate Criteria). Is there any existing stru...

LINQ: How to skip one then take the rest of a sequence

Hi All, i would like to iterate over the items of a List<T>, except the first, preserving the order. Is there an elegant way to do it with LINQ using a statement like: foreach (var item in list.Skip(1).TakeTheRest()) {.... I played around with TakeWhile , but was not successful. Probably there is also another, simple way of doin...

Get sum of two columns in one LINQ query

Hi, let's say that I have a table called Items (ID int, Done int, Total int) I can do it by two queries: int total = m.Items.Sum(p=>p.Total) int done = m.Items.Sum(p=>p.Done) But I'd like to do it in one query, something like this: var x = from p in m.Items select new { Sum(p.Total), Sum(p.Done)}; Surely there is a way to call agg...