linq

LINQ to SQL - is there a way to get a 'preview' of the database with changes made to a context?

I have a program in which I am performing non-atomic updates to the database with a single DataContext (that is, the changes are submitted when the user clicks Save, not after each transaction). When the user saves, I would like to be able to query the database (as it would be when I call submitChanges()) and do some checks just before ...

Linq: "Or" equivalent of Where()

Is there a method in Linq where you can use to build SQL strings like "...where (a=1) OR (a=2)"? ...

Using an IEqualityComparer with a LINQ to Entities Except clause

I have an entity that I'd like to compare with a subset and determine to select all except the subset. So, my query looks like this: Products.Except(ProductsToRemove(), new ProductComparer()) The ProductsToRemove() method returns a List<Product> after it performs a few tasks. So in it's simplest form it's the above. The ProductCompa...

How can this query be improved?

I am using LINQ to write a query - one query shows all active customers , and another shows all active as well as inactive customers. if(showall) { var prod = Dataclass.Customers.Where(multiple factors ) (all inactive + active) } else { var prod = Dataclass.Customers.Where(multiple factors & active=true) (only active) } Ca...

Linq conversion

I am using the following code to return an IList: public IList<string> FindCodesByCountry(string country) { var query = from q in session.Linq<Store>() where q.Country == country orderby q.Code select new {q.Code}; return (IList<stri...

SQL to LINQ converter required, any idea ?

We have LINQPad which is great for testing Linq expressions targeting database. Can any one recommend "free ware" tool, that could help us convert "Sql Queries" to LINQ expressions and why you recommend it? NOTE: Please keep in mind, we use c#. ...

LINQ group by month question

I'm new to LINQ to SQL and I would like to know how to achieve something like this in LINQ: Month Hires Terminations Jan 5 7 Feb 8 8 Marc 8 5 I've got this so far, and I think there is something wrong with it but I'm not sure: from term1 in HRSystemDB.Terminations ...

How to instantiate an object within a linq query

This is kinda theoretical question, I was looking at someone else' code (below) and my simple solution was to instantiate the collection outside linq, but I can guess there will be cases where I'd want to instantiate the objects inside the query, and perhaps only on a selection of elements. Here's a simplified example of how this was be...

Can LINQ To SQL generate invalid SQL?

I have two tables that I am using Linq to SQL with. The tables have a 1 to many association. The important part of the database schema is as follows: Camera: Id (int) SerialNumber (string) ... CameraCalibration Id (int) CameraFk (int) ... Using LINQ to SQL I can obtain a list of all Camera Calibrations for cameras with 10...

How does this LINQ Expression work?

I know what this code is doing but I'm not sure on the syntax. It doesn't seem to conform to a "standard" format. Is it mostly LINQ? return db.Subjects.SingleOrDefault(s => s.ID == ID); The first part makes sense but it's the part in the brackets I don't understand. How can we use s without declaring it? And how are we putting logic i...

xml to sql using linq and C#

I have an xml file from an external system that looks like this. <?xml version="1.0" encoding="UTF-8"?> <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"&gt; <Body> <Element1> <Element2 day="2009-10-18"> <Element3 name="Joe"> <Element4 time="1"> ...

How to skip(m).take(n) from a List<T> ?

Given: List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; How do I implement the following code? var list2 = list.skip(2).take(5); ...

Is there any Linq like feature in any languages prior to .NET?

I was wondering, if there are examples of Linq like features in other languages and programming platforms prior to .NET or this was the invention of Microsoft. ...

SQL Database with XML fields, select according to XElement using LINQ

I have a table in an SQL database, where the table configuration corresponds to ID Name string data xml where the datafield might (but not necessarily) contain a descendant element <config>Some value...</config> Using LINQ I want to select all the rows that has a data xml element which contains the config element with a value of......

StringTemplate can not render attribute of LINQ to SQL class

i use GUI tool of vs2008 to generate some LINQ to SQL class, my problem is StringTemplate can not reach attributes of those model $persons:{ <li>$it.name$</li> }$ it printed: <li></li> <li></li> <li></li> name is public property of Person model. If i create a person class by myself, and the same attributes, StringTemplate can get i...

how to discard changes made to all linq tables?

I would like to discard all changes made to linq tables (this means -- I use linq, and data are changed on client side, the data on server are intact). How to do this? EDIT: problem partially solved http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext/ It works as long as you don't use transaction. When you do and you use m...

Is there a way to use Linq projections with extension methods

I'm trying to use AutoMapper and a repository pattern along with a fluent interface, and running into difficulty with the Linq projection. For what it's worth, this code works fine when simply using in-memory objects. When using a database provider, however, it breaks when constructing the query graph. I've tried both SubSonic and Lin...

Missing nodes cause a null exception crash linq to xml

I get a null exception on this because MYTAG1 doesn't exist. I understand that this is because Element("MYTAG1") is null and calling Elements("MYTAG2") on it wont work. How do I deal with this to prevent the crash? var myItems = from myNode in Nodes.Element("MYTAG1").Elements("MYTAG2") select new EPTabl...

Very simple LINQ query, passing in textbox value to the where clause

I want my LINQ query to count the number of instances a member is present in a table based on what the user enters into a textbox. As a result, I have the following protected void btnSubmit_Click(object sender, EventArgs e) { LoginDataContext lg = new LoginDataContext(); int logincheck = (from r in lg.tblOnlineReportingLogins ...

Get Alphabetical List Of Items in Collection Using Lambda/Linq?

I have a list of objects. Each object contains a property called 'DisplayName'. I want to create another list of string objects, where each string represents the first letter or character (could be a number) in the DisplayName property for all objects in the initial list, and I want the list to be distinct. So, for example, if my list...