linq-to-sql

How to retrieve indentity column vaule after insert using LINQ

Could any of you please show me how to complete the following tasks? // Prepare object to be saved // Note that MasterTable has MasterTableId as a Primary Key and it is an indentity column MasterTable masterTable = new MasterTable(); masterTable.Column1 = "Column 1 Value"; masterTable.Column2 = 111; // Instantiate DataContext DataCont...

Sequential GUID in Linq-to-Sql?

I just read a blog post about NHibernate's ability to create a GUID from the system time (Guid.Comb), thus avoiding a good amount of database fragmentation. You could call it the client-side equivalent to the SQL Server Sequential ID. Is there a way I could use a similar strategy in my Linq-to-Sql project (by generating the Guid in code...

Does SQL Server cache LINQ to SQL queries?

As far as I know, in MS SQL Server 2000+ stored procedures are compiled, and run faster than normal non-compiled queries. I wonder if MS SQL Server also compiles LINQ to SQL queries, and caches that compilation, for performance purposes. ...

LINQ Database

I have a system that supports multiple products. Each product has its own database with the same exact schema. When I pass in the connection string as a parameter to my Data Context constructor it always uses the default database listed in the connection string, or the default database of the user connecting if I do not provide an Initi...

Best way to update in Linq To SQL

Hello All, I have several entity classes that I use for parsing fixed width text files as well as utilizing Linq to SQL. I use these classes to parse data from said text files, and compare to data in the database. One of these entities has a lot of properties, and I don't want to waste time setting each individual property on the Linq r...

Is it possible to run arbitrary queries with Linq to SQL? (C#)

How would I write the following query using Linq to SQL? UPDATE [TB_EXAMPLE] SET [COLUMN1] = 1 (My actual goal is more complex than this) ...

ADO.NET MVC Tutorial (with LINQ to SQL) question about schema changes

So I'm working through the new scottgu wrox book, and I create a couple tables. Then (per the tutorial) I use linq to sql to build my Models, DataCOntext, Repository, etc. (It's pretty clear that LINQ to SQL seems to be "the Microsoft Way" to generate models and similar classes from schemas.) Then I find I've neglected to include one of...

Linq to Sql: Where condition order impact

With a LINQ-TO-SQL linq query - does the SQL generated respect the 'Where conditions' order? For instance: int[] result = (from r in DB.Accounts where r.AccountID > 10 && r.Name == "Harry").ToArray(); If there are thousands of rows, and there is an Index on the Name column, will the SQL query by the Indexed column fi...

How do you handle Foreign Key Relationships in model classes

Let's say I have a Project table with a FK CompanyId which relates the project to a company Table. In your Project model, do you add a Company object, or just the CompanyId property and retrieve the Company when needed in code? ...

Restrict LINQ Subquery when databound to a grid

I want to limit the number of child elements I get back. In this example Order.CustomerID "VINET" has 3 Order Details. I only want to see the record that has a unit price of 14. I do NOT want to see the Order Details where the unit price equals 9.8 or 43.8 In the end I want to do this in a dynamic where query or with a predicate, but...

Order by with Collate clause | Entity Framework

I don't want to use array sorting on the web server it should be done on SQL server. Microsoft does not support query(in EF) like this: SELECT * FROM [Table_1] ORDER BY [Table_1].field COLLATE SQL_SwedishStd_Pref_Cp1_CI_AS Any ideas? Thank you in advice... ...

Problem with Linq2sql quey

I'm having trouble with following linq2sql query: public IEnumerable List(IQueryable<Enquiry> enquiries, Supplier supplier) { IEnumerable result = from e in enquiries let order = supplier==null ? null : e.Orders.Where(f => f.ClientId.Equals(supplier.Id)).FirstOrDefault() let enqui...

LINQ to SQL vs ADO.Net

What's the difference between LINQ to SQL and ADO.net ? ...

How to get a tree structured table data by linq?

I have a table which has a tree structure in itself. Id ParentId Name ---------------- 1 null x 2 null y 3 null z 4 null t 5 1 xx 6 1 xy 7 1 xz 8 2 yx 9 2 yy 10 9 yyx 11 10 yyxx 12 11 yyxxx I want to retrieve the whole sub-tree under a root node. When my r...

Question about Repositories and their Save methods for domain objects

I have a somewhat ridiculous question regarding DDD, Repository Patterns and ORM. In this example, I have 3 classes: Address, Company and Person. A Person is a member of a Company and has an Address. A Company also has an Address. These classes reflect a database model. I removed any dependencies of my Models, so they are not tied to a...

how to assign default value to timestamp datatype in c#?

i want to do above. i have an object of table in c#. ...

Linq to Sql Parent Child

ok, so I new to the C# way of doing things, I come from the ruby world. I have a one to many relationship, (parent to children for sake of this question), and for some reason L2S was wanting to create a new parent instead of using the one is already had. Here is the code. Console.WriteLine(parent.Id); // this equals 1 foreach (string...

ASP.Net MVC - Null Objects in Views

I'm looking for a clean way to handle null object references in LINQ to SQL model class when they are passed to a View. Simple example. TableA has a FK into TableB. The FK relationship may or may not exist for any row in TableA. My LINQ to SQL classes express this relationship as ClassA.ClassB.Property, but in some instances ClassA.Cl...

What's the point of DatabaseAttribute within LinqToSQL?

A LinqToSQL Context class is annotated with DatabaseAttribute by default. It is optional however. In any scenario I can think of you would use the connection string to point at a database, so what's the reason for it. Now, I've obviously read docs. which state, you use it to specify a default database if you wish to omit it from the conn...

How can I define my own columns in a WPF DataGrid?

I've got a AutoGenerateColumns WPF-DataGrid getting bound in code-behind to LINQ-to-SQL, which works fine. But when I take off teh autogeneratecolumns and define my own columns, it tells me "The items collection must be empty before using ItemsSource." But I'm not binding the ItemSource in my XAML so I don't see why it isn't empty. Wha...