linq-to-sql

Question about LINQ2Sql performance in C#

Hey SO: From a performance perspective, is it better to wrap each statement that utilizes LINQ in a using() statement, or to declare a class-level instance and use in each method? For instance: public void UpdateSomeRecord(Record recordToUpdate) { using(var entities = new RecordDBEntities()) { // logic here... } } p...

Linq and nullable key relationships affecting UNION operation

Here is an example: Lets say I have 3 tables, Countries, People and Cities. City records have a non-nullable foreign key field identifying a country. People records have a nullable foreign key field identifying a country - they may be from an Eastern European country that no longer exists. I want to create a combined list of countries ...

LINQ2SQL - Cross join emitted when I want inner join

This emits inner joins, which is what I want and works: var q = from itm in esdc.items join itmImg in esdc.itemImages on itm.itemId equals itmImg.itemId join itmIdent in esdc.itemIdentities on itm.imgIdentityId equals itmIdent.itemIdentityId join startImgs in esdc.vStartPgImgs on itmImg.imgId equals startImgs.imgId s...

Linq Take() question

I want to filter my results to take only the X amount of records. I am wondering how does Take() work? On this site I found: http://www.hookedonlinq.com/TakeOperator.ashx It says Take() "Throws an ArgumentNullException if source is null." So what should I do? I can't guarantee that everytime I do a Take() I will have some records in t...

LINQ to SQL Decimal Parameter

I have a very simple linq to sql query in C#: int acctNum = 12345; var query = from p in db.table where p.ACCT_NO == acctNum select p; This generates the following SQL: exec sp_executesql N'SELECT [t0].field1, [t0].field2, [t0].ACCT_NO FROM [dbo].[table] AS [t0] WHERE [t0].[ACCT_NO] = @p0'...

Should A repository call another repository? Or should a repository call a service layer?

Hi I am trying to figure out how to tackle this problem. I have to insert some data into 2 tables lets call them Table A and Table B. Table A has these columns AId<PK> A1 A2 A3 Table B has AId<PK> A1 B2 B3 B4 Now my first question was should another repository call another repository? I don't think this will solve my current proble...

DataContext's Connection string help

I have the following connection string: "Data Source=localhost\\SQLEXPRESS;", it complains that '' is an invalid username. I can't quite remember the user name or password commands, nor can i remember if there's anything else i'm missing. Can someone help? ...

Add data to a Table<T>?

I have a property on a class that happens to be a Table. How do i add an item to this? ...

Best practices when limiting changes to specific fields with LINQ2SQL

I was reading Steven Sanderson's book Pro ASP.NET MVC Framework and he suggests using a repository pattern: public interface IProductsRepository { IQueryable<Product> Products { get; } void SaveProduct(Product product); } He accesses the products repository directly from his Controllers, but since I will have both a web page a...

LINQ-to-SQL Enumerable expression with Binary = null fails

This is a strange LINQ-to-SQL problem which can't evaluate as an Enumerable (in SQL) but I can evaluate client side. I think it's related to my testing Binary property as 'null'. I need to determine when my Job is complete, which means that all Files in that Job have at least some data in their binary FileContents property. The proced...

Loading data from xml

Hi, In my program, there are two class, say, category and product, with each object of product hold a reference to the category it belongs to. When loading data from XML and store them in the database, a problem arise: If I create a category object and store it in the database first, there will be dirty data when creating product faile...

Finding maximum number of rows created per hour?

I have a table to store product reviews like this: Id -int ProductId -int Timestamp -datetime Comments -text Is there an easy way to count and determine the rate of reviews a product has received in any 60 minute timespan? ie. Widget1 maximum reviews/hour is 55. working with sql05. ...

How to use factory classes with linq for sql?

Hi, I have a model on top of my database model and map the objects in my Repository. However, apparently it makes a difference whether I "select new" directly in my GetUsers or "select factoryresult" as implemented below. I get the error at runtime, that the method CreateFromDbModel does not have a translation to sql (System.NotSupport...

LINQ many-to-many relationships: Solution?

LINQ so far has been remarkably elegant, but to perform basic m2m queries it offers no solution I can imediately see. What's worse is that while it works for any other table relationship, LINQ is not giving me an association in the class structure for my m2m table. So I can do things like artwork.artists.where(...) //or artist.Artwo...

LINQ Column Attribute not found

I am a beginner in LINQ. I am using .NET 3.5. and VS 2008. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq.Mapping; using System.Data.Linq.Provider; namespace LINQ_to_SQL_Test { [Table(Name="Person")] public class Person { [Column(Name="ID", Storage="_ID...

Selecting records using Linq2sql into linq2sql classes?

Hi there, Been succcesfully adding records using Linq2Sql with the linq2sql genereated classes, works great.. But i need to now select the records, i can't seem to figure this out This is how i am adding records - reservation is a generated linq2sql class TestDataContext db = new TestDataContext(); db.Reserva...

LINQ relationships (The burning question)

What does it actually mean by the term "Entity" in LINQ? What is the difference between EntitySet and EntityRef when talking about LINQ? Can you give a real-world example? Like Order and OrderItems, etc? ...

LINQ syntax where string value is not null or empty

I'm trying to do a query like so... query.Where(x => !string.IsNullOrEmpty(x.PropertyName)); but it fails... so for now I have implemented the following, which works... query.Where(x => (x.PropertyName ?? string.Empty) != string.Empty); is there a better (more native?) way that LINQ handles this? EDIT apologize! didn't include t...

Linq To Sql and identity_insert

I am trying to do record inserts on a table where the primary key is an Identity field. I have tried calling mycontext.ExecuteCommand("SET identity_insert myTable ON") but this doesn't do any good. I get an error saying that identity_insert is off when I submit changes. How can I turn it ON from the c# code before I submit changes? E...

Data Repository - business objects?

I'm reading the book "ASP.NET 3.5 Social Networking - Andrew Siemer" and I got confused when he uses Repositories to access the data. Here is the idea of his code: public interface IAccountRepository { Account GetAcountByID(int acId); void SaveAccount(Account account); List<Account> GetAllAccounts(); } public class Account...