linq-to-sql

What is a good naming convention for domain objects versus their relational tables?

I have seen this issue twice now and want to see what a best practice is for resolving this naming convention. Suppose I have an ORM (Linq to SQL in this case) and I map a table called User. Then I create my POCO called User also. Although the mapped table exists in something like MyApp.Data.User and the POCO resides in something l...

Linq to SQL in ObjectDataSource w/ GridView: IEnumerable manual sorting vs DataTable Auto-sorting?

The setup: I use Linq to Sql for my DAL and I extend the classes to encapsulate my CRUD methods (create, read, update, delete) for my BLL. I don't just use the LinqDataSource because I usually have to run some other subroutines whenever I do my normal CRUD stuff (I know, this is pretty standard), so I use the ObjectDataSource control...

Linq2sql inheritance cast

I'm usign inheritance in linq2sql and have entites Supplier and Buyer that inherits from Client. I need to be able "upgrade" a Buyer to a Supplier. I've tried: Client client = ClientMethods.ValidateId<Client>(clientId); client.ClientTypeId = ClientMethods.CLIENT_TYPE_SUPPLIER; db.SubmitChanges(); But get "Not...

Full Text Search in Linq

There's no full text search built into Linq and there don't seem to be many posts on the subject so I had a play around and came up with this method for my utlity class: public static IEnumerable<TSource> GenericFullTextSearch<TSource>(string text, MyDataContext context) { //Find LINQ Table attribute object[] info = typeof(TSour...

Linq to Sql - Select from 1:Many

I'm new to using Linq and just started a side project to learn some of the basics. I'm currently using Linq to Sql and all my DB Table relationships worked very well. Currently I have a Client table and a Project table. Each Client can have 1 or more Projects. Therefore, as you'd expect each Client object has a collection of Project obje...

LINQ Expression to return Property value?

I'm trying to create a generic function to help me select thousands of records using LINQ to SQL from a local list. SQL Server (2005 at least) limits queries to 2100 parameters and I'd like to select more records than that. Here would be a good example usage: var some_product_numbers = new int[] { 1,2,3 ... 9999 }; Products.SelectByP...

Dynamically searching using LINQ and dynamically created predicates

I have some predicates being dynamically built that have the following signature passes through as parameters into a function: Expression<Func<TblTableR, bool>> TableRPredicate, Expression<Func<TblTableN, bool>> suspectNamesPredicate, Expression<Func<TblTableS, bool>> TableSPredicate, Expression<Func<TblTableI, bool>> suspectTableIPredi...

C#, Linq2Sql: Is it possible to concatenate two queryables into one?

I have one queryable where I have used various Where and WhereBetween statements to narrow the collection down to a certain set. Now I need to add kind of a Where || WhereBetween. In other words, I can't just chain them together like I have up till now, cause that will work as an And. So, how can I do this? I see two possibilities: Cr...

LINQ to SQL - Decimal datatype truncated instead of rounded

I have a DB column of decimal(6,1) When saving a record with LINQ to SQL, it truncates the value instead of rounding. So for example, if a parameter passed to this column in the generated LINQ to SQL query has this value... -- @p1: Input Decimal (Size = 0; Prec = 6; Scale = 1) [222.259] the data will show up in the DB as 222.2 instead o...

CRUD operations on linq when datacontext ObjectTrackingEnabled = false

Hello all, A small question. I am using LinqToSql in an n-tier application. I have a datacontext, and I wish to manipulate CRUD operations. I'd rather my datacontext not have object tracking for reasons of scalability. But then, when I have object tracking set to fault I cannot use the built in CRUD operations the datacontext has to off...

LINQ to SQL: Insert non-identity Primary Key

I am using LINQ to SQL for the first time on a small project. My tables have Primary Keys that are not identity columns because I am essentially importing from another database. The relationship is a many-to-many with a linking table. Is there a way to tell LINQ to ignore the insert if it is a duplicate? I have already tried to check th...

What's the best way to commit a serialized JQuery sortable result to the database?

I am using the JQuery.Sortable() function to create a reorderable list of items like so <script type="text/javascript"> // When the document is ready set up our sortable with it's inherant function(s) $(document).ready(function() { $("#goal-list").sortable({ handle: '.handle', placeholder: 'ui-st...

LINQ to SQL - Grouping by hours

Hi, How can I group result of a LINQ to SQL query by hours considering that the column type is DateTime? ...

Assigning Roles to Users

I'm having trouble figuring out how to assign roles to users. I've got the log in mechanism working, but need to figure out how to use roles defined to give users certain access. I have a database with these tables: Tables ------ UserTbl RolesTbl UserInRoleTbl ------- ...

Need a good example of LinqDataSource in code, not markup

Anyone have a good example of setting up a LinqDataSource entirely in code? I don't need help writing the LINQ query. I just need help setting up the flow of the code. The reason I want to do it in code is because the complexity of the query I need goes beyond the capabilities of the LinqDataSource wizard. ...

Run web app code from command line?

I have an ASP.NET web application that includes code for enforcing its own database schema ; this code runs on application start. I've recently started using LINQ to SQL, and I've added a pre-build event to run SqlMetal on my database so that I get objects representing my db tables. What would be really cool is if I could enforce the ...

C#, Linq2SQL: Sum() causes exception instead of returning 0 when no rows

I have this code (ok, I don't, but something similar :p) var dogs = Dogs.Select(ø => new Row { Name = ø.Name, WeightOfNiceCats = ø.Owner .Cats .Where(æ => !æ.Annoying) .Sum(æ => æ.Weight), }); Here I go through all dogs and sum up the weight (into a non-null...

In LINQ to SQL, is InsertOnSubmit() required when adding via a Foreign Key?

In LINQ to SQL, is InsertOnSubmit() required when adding via a Foreign Key? I.e. If I have code that does the following, where orders and order lines are linked in the dbml and database via a foreigh key: Create order. Add order to datacontext orders. Create order line. Add to order.Lines. Do I still need to add the order line to th...

Easy way to add an ID column when there is data present

Is there an easy way to add an ID (Identity(1,1) & PK) column to a table that already has data? I have picked up a project that was freelanced out to a horrible developer that didn't put a PK, index or anything on the tables he made. Now that I am LINQ-ifying it, I have no PK to insert or update off of. ...

How do I include an InvocationExpression that is only evaluated once in an expression tree for linq to sql?

I'm trying to manually combine expression trees to accomplish a level of modularity that seems to allude me using the standard linq operators. The code essentially creates an expression tree that uses one expression to decide which of two other expressions to call. One of the other expressions requires an extra parameter that is itself o...