[HowTo] Linq Pluralize
Sometimes Linq add or remove ‘s’ to the objecs, this is an option availlable in : Tools -> Options -> Database Tools -> O/R Designer
[HowTo] [SQL] Insert data in a column with default value with Linq
Il suffit de jouer avec les paramêtres AutoSync et IsDbGenerated dans les propriétés du champs que l’on veut traiter. il suffit de mettre la valeur AutoSync.OnInsert dans la propriété AutoSync et true dans la valeur IsDbGenenrated Voici plus de détails : [Column( Storage=”_Id”, AutoSync=AutoSync.OnInsert, DbType=”Int NOT NULL IDENTITY”, IsPrimaryKey=true, IsDbGenerated=true )] Ces paramètres sont accessibles […]
[HowTo] Select Top x using Linq
If you wish to make such a query : Select top 10 Foo from MyTable use the .Take(x) method : var foo = (from t in MyTable select t.Foo).Take(10); In VB LINQ has a take expression: Dim foo = From t In MyTable Take 10 Select t.Foo
[HowTo] Linq Count Granchild items from an object with .SelectMany()
Lets say you have the following structure : Product -> Zone -> Category -> Item Meaning a Product has multiple Zones with multiple Categories with multiple Items Lets say we need to select the count of items for each product in a simple distinct list of product. Linq query using .SelectMany() : xyzDataContext dc = […]
Handling Multiple Result (Shapes) from SPROCs
Handling Multiple Result Shapes from SPROCs [Function(Name = “spGetWebAccount”)] [ResultType(typeof(Client))] [ResultType(typeof(User))] [ResultType(typeof(UA_User))] public IMultipleResults spGetWebAccount(System.Nullable<Int64> ClientID, System.Nullable<Int64> UserID) { IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), ClientID, UserID); return (IMultipleResults)result.ReturnValue; } Source : http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx
[LINQ] The query results cannot be enumerated more than once (using databinding…)
When you bind the ISingleResultfor the first time, the results are enumerated and the data object is populated. When you bind the same ISingleResult instance to the same control again, it detects that the data-source is the same as its last source (by comparing their object references) and does not enumerate the ISingleResult instance for […]
Dynamic where clause using Linq To SQL
var query = from c in context.Customers select c; if (name != null) query = query.Where ( customer => customer.Name == name ); if (contactName != null) query = query.Where ( customer => customer.ContactName == contactName ); This way we can pass different combinations of arguments to the method and it will still build the […]
Linq to SQL Like Operator
Starting from a simple query from Northwind Database; var query = from c in ctx.Customers where c.City == “London” select c; The query that will be sent to the database will be: SELECT CustomerID, CompanyName, … FROM dbo.Customers WHERE City = [London] There are some ways to write a Linq query that results in using […]
Visual Studio DBML and SQL column default value
Usually when creating a database table, you will tend to add “default values” to some columns (Guid, Dates, …). BEGIN ALTER TABLE [dbo].[User] ADD DEFAULT (getdate()) FOR [Created] END Adding the table to a DBML file under visual studio will ‘by default’ disallow the default value in the table. The mapping with default values will […]
DataGridView datasource : Use BindingSource or direct query ???
LINQ to SQL translates LINQ queries to SQL for execution on a database. The results are strongly typed IEnumerable. Because these objects are ordinary common language runtime (CLR) objects, ordinary object data binding can be used to display the results. On the other hand, sorting and change operations (inserts, updates, and deletes) require additional steps. […]