linq

C# List Comprehensions = Pure Syntactic Sugar?

Consider the following C# code: IEnumerable numbers = Enumerable.Range(0, 10); var evens = from num in numbers where num % 2 == 0 select num; Is this pure syntactic sugar to allow me to write a for or foreach loop as a one-liner? Are there any compiler optimizations under the covers that make the list comprehension above more efficien...

Handling different databases with LINQ based off the user which is current logged in?

I've been given the task of creating a site that will allow our various large clients to log into our website and click on our various pages to view analytics data based on their sales. Does anyone have any idea about the best way to handle multiple databases based off the user? Lets say we have 3 big name clients, the design decision ...

Flatten Linq to XML query result from dynamic data

In a database the data is stored dynamically. That means the metadata defines classes and attributes. The user data is stored in object and object attribute tables. To simulate how the data is represented, I created a linq query in LinqPAD: XElement test = XElement.Parse ( @"<DynaDaten> <CLS ID='99' CODE='T_PERSON' NAME='T_PERSON...

Why does DataGrid call Linq query when scrolling?

I am just getting started with Linq, WPF and Silverlight. I am trying to display data which originates from a XML document in a DataGrid. I use a Linq query to select the objects I want and link the result to the DataGrid. XDocument doc = GedView.GedcomConverter.ConvertToXml(new StreamReader(e.Result)); var query = from person in doc.D...

C#: Creating objects that does not exist in a different list

I have two lists of two different kinds of objects representing data rows from two sql queries. The first list contains data, and the second contains more detailed data. So as an example: List1: List2: 1 Alice 1 15 2 Bob 1 19 3 Carol 2 5 4 Dave 2 7 2 20 4 ...

Howto create a System.Linq.Expressions.Expression for Like

I created a filterable BindingList from this source: http://www.nablasoft.com/alkampfer/index.php/2008/11/22/extend-bindinglist-with-filter-functionality/ It works great ( list.Filter("Customer == 'Name'"); does what it should. In the internals works a parser, that converts the expression "==" or "!=" into "System.Linq.Expressions.Exp...

Dynamic LINQ query on List with complex data type (collections)

Hi, I have a situation where I would like to create a dynamic LINQ query where the fields are generated on the fly. following code snippet show what I am trying to achieve. First query is a standard LINQ query on objects. Second query is a Dynamic LINQ query on objects. Second query fail at "Selected = false", and "(string)rb.User...

How can I use linq to sort by multiple fields?

I'm creating a mock data source that I want to be able to pass in a list of SortExpressions on. public SortExpression(string name, SortDirection direction) { this.name = name; this.direction = direction; } Update with Jon Skeet's code and also the entire class. GetData() is just populating the object with x number of record...

Detect a DB Connection error using Linq?

Is it possible to detect an error in a connection before we get an application "Transport-level error" using Linq? Lets say that I have a SQL server called SQLServer1, and I am running an application that connects to that server using LinQ to get some information. I decide to change the server Name to SQLServer2. The next time my appl...

Are the following Lambda and Linq expressions equivalent?

Hi Everyone, Are the following Lambda and Linq expressions equivalent in terms of execution paths? I guess I'm wondering if the Linq is going to run differently because it's going to create an IEnumerable before determining if the enumeration has anything in it whereas the lambda expression will stop on the first digit it finds. var x ...

Custom Generic GetTable

For a DataContext I'm working on, I don't want to load the Tables untill their needed. I want to make a little method that would check if a certain Table is loaded before loading it, but I end up having n Methods doing the same thing : private void Load(ref Table<Order> Orders) { if (Orders == null) Orders = this.GetTable<Or...

How can I make a primary key as AUTOINCREMENT in vb.net 2008?

I have table in a DB and the primary key is the 'TID' column, and I want to make it as AUTOINCREMENT, but I can't, because the "Identity specification" is turned off! See this picture: http://www.rofof.com/img2/6xsbuy6.gif How can I do it? Thanks! ...

Where IN clause in LINQ

How to make a where in clause similar to one in SQL Server? I made one by myself but can anyone please improve this? public List<State> Wherein(string listofcountrycodes) { string[] countrycode = null; countrycode = listofcountrycodes.Split(','); List<State> statelist = new List<State>(); for (i...

how to set IDENTITY_INSERT as ON ?

in the first I ask the admin to remove this post: 'how-can-i-make-a-primary-key-as-autoincrement-in-vb-net-2008/959787#959787' because I am facing a problem with comment and when I try to add comment the error message is apper: "commenting requires 50 reputation -- see faq" ! Here just I want to ask about IDENTITY_INSERT, how can I set...

IEnumerable<T> evaluation and unit testing

After reading another SO thread (http://stackoverflow.com/questions/396513/should-parameters-returns-of-collections-be-ienumerablet-or-t), I am of the opinion that a unit testing 'issue' I'm having is probably related to what the thread refers to as "late evaluation". I am passing three different but related flavors IEnumerables from a ...

"Where In" with linq to sql

I am trying to provide the user with a list of items in a database table upon entering keywords in a textbox. The problem i currently have is that the user can enter several keyword and i would like to be able to perform only one roundtrip to the database and get back results matchings several keywords (an OR operation). Is there a way t...

Create LINQ Association without Foreign Keys

Is it possible to have something like ContactAddress.Contact in LINQ without create a foreign key relationship in SQL Server between those two (which would by Contact.Id <-> ContactAddress.ContactId)? Thanks :) ...

Stack overflow in LINQ to SQL and the Contains keyword

I have an Extension method which is supposed to filter a Queryable object (IQueryable) based upon a collection of Ids.... Note that IQueryable is sourced from my database via a LinqToSql request public static IQueryable<NewsItemSummary> WithID(this IQueryable<NewsItemSummary> qry, IQueryable<Guid> Ids) { return from newsIt...

Error: Both DataSource and DataSourceID are defined on 'GridView2'. Remove one definition. ?!

when I write this query (using LINQ): Dim hw22 = From hw In db.HWs _ Select hw then: GridView2.DataSource = hw22 GridView2.DataBind() this error occurs: http://www.rofof.com/img2/6hscmu7.gif How can I solve this? ...

Adding data into MS SQL Database from several classes using LINQ?

Ok, so I am not even sure where to begin here. SQL programming is fairly new to myself. Also before I go on about my issue I would like to be able to complete this in LINQ and not using stored procedures. Problem: I have multiple nested classes and I need to be able to add the classes to several tables in a sql database. I need to also...