linq-to-sql

how to could use reflection to write to the appropriate properties on the entity?

How to: you don't want to manually write code to write to ColumnA, ColumnB, etc, then you could use reflection to write to the appropriate properties on the entity? you can create a new instance of this class and its property values. These property values are mapped to columns in the SQL database table. You then pass this object to the D...

LINQ update procedures - best practice

What would be considered best practice for updates when using LINQ: To have an instantiated db/facade object, and submit all changes to a record in one go? To have a static db/facade object, and submit changes cell by cell? Right now I'm in a project where each cell for a record has its own static update method public static void Ce...

How to increase SQL Server database performance?

Hello, I have table in a SQL Server database with only 900 record with 4 column. I am using Linq-to-SQL. Now I am trying retrieve data from that table for this I have written a select query. Its not querying data from database and its showing time out error. Please give me idea for this. First how can I increase time and second how c...

how to use dynamic insert - linq to sql?

i want to use below codes. So i have a generic list<MyClass> i think that i should use using System.Reflection; method to get list<MyClass> property to fill string[] columnNames, object[] columnValues but how can i use it? foreach (var item in List<myClass>) { // i want to fill get columnNames and values to fill below array...

Dynamic creation of models - entities using Asp.Net MVC

So, i have my database sorted like: Products_001 Products_002 Products_003 Let's say, if a customer logs in and his id is 001, he is only entitled to use the database-table products_001. Can i dynamically create his model - entity using Asp.Net MVC, and how would i do this? ...

DateDiff in LINQ

Hi Hello how I can find datediff in month using LINQ.. thanks... ...

LINQ SubmitChanges() Behaviour

Hello, I have a Windows Form application using LINQ to SQL. It's working all good and fine but there is one thing I fail to understand. In one of the forms, I have a DataGrid which shows a list of order items. When I save them using SubmitChanges(), in the first call (after the application has been run) it saves the ALL the order item...

Inserting programmatically but i can not do that in linq?

How can i insert data programmatically? but i can not do that. No error return also no adding data in my database.How can i do that? i created a Extention method to use in my project look please below.i want to add extentionmethod but i can not. How can i add data with below extention method? namespace TestEng { public partial class...

Linq and SubSonic - returning nested complex types

Hi all, I'm new to SubSonic and reasonably new to LINQ as well, so I'm just trying to put a little app together. I've got the templates all sorted and running okay, but I've run into a bit of trouble with this LINQ statement (simplified slightly, the real statement has some other joins but they don't affect this particular problem so I'...

LINQ to SQL - Association lost when I turn ObjectTrackingEnabled off

I am using Linq-to-SQL (C# 3.5) to read two tables, house and county, from my database (SQL Server 2000). House has a CountyID column that matches an ID column in the county table. The County table has ID and CountyName columns.There is no association in the database - I added it in the dbml file. (Parent class = County, Child class = Ho...

C# CodeSmith LINQ to SQL question DELETE Operation

In a project I am working on I need to delete a "user" from my database. This "user" has two tables that reference it's foreign key. When hard deleting I am trying to delete all records from Table A and Table B that have foreign keys to the "user" and then deleting that "user" record. This is all done within repositories and using obj...

Is this really a possible InvalidOperationException?

I have a Linq2Sql query that looks like this: var data = from d in dc.GAMEs where (d.GAMEDATE + d.GAMETIME.Value.TimeOfDay) >= DateTime.Now && d.GAMESTAT == 'O' && d.GAMETYPE == 0 select d; Resharper is underlining the "d.GAMETIME.Value.TimeOfDay" in blue and telling me it's a possible System.InvalidOperationException. Wh...

Reusing Expression in Linq select clause (query format)

I have an Expression that converts one type of object to another type. The expression is as follows: public Expression<Func<SQLRepository.ActionType, Model.ActionType>> DBActionTypeToActionType = (SQLRepository.ActionType at) => new Model.ActionType() { ID = at.OID, DisplayName = at.DisplayName }; I can use the Expression like...

LINQ Statement Not Updating Database?

I have the following code: Dim dbHousing As New dcHousingDataContext Dim pullresidents = From p In dbHousing.webResidents _ Select p.dorm_building, p.dorm_room, p.occupantnum _ Order By dorm_building, dorm_room Dim j as integer = 1 Dim previous As String = "" For Each row In pullre...

How to generate better SQL from Linq2Sql query

I have the following query: var data = from d in dc.GAMEs where (d.GAMEDATE + d.GAMETIME.Value.TimeOfDay) >= DateTime.Now select d; This generates some horendous looking SQL, looking something like this: SELECT {...} WHERE DATEADD(ms, ((CONVERT(BigInt,((CONVERT(BigInt,DATEPART(HOUR, [t0].[GAMETIME]))) * 36000000000) + ((CONVERT...

Get inserted/updated/deleted rows count from stored procedure - Linq to SQL

I'm calling a stored procedure which does some updates/inserts/deletes (any one of these at a time) from Linq. This stored procedure is added to datacontext I'm using. After calling this stored procedure I want to get the number of rows affected by this stored procedure. This stored procedure may affect more than one table also. I tried...

Random DataReader errors and thread-specific DataContext allowing for changing DataLoadOptions.

My .NET MVC project has reached the stage of testing with multiple users and I am getting seemingly random errors (from any screen in the site) relating to Linq2Sql DataReader issues, such as: 'Invalid attempt to call FieldCount when reader is closed.' and 'ExecuteReader requires an open and available Connection. The connection's current...

How can I create a dynamic select expression in LINQ?

What I currently have looks a bit like this: if(userLikesBananas) { return from fruit in basket select new Fruit { AteBanana = Bowl.Any(b => b.OwnedBy == user && b.Contains(fruit) && fruit.Type == FruitType.Banana), ... ... //lots of properties ...

Linq-sql classes to select using max

i have a table Employee it contain fields ename,eid,workid,date,work etc.. which store the work assigned to an employee with workid as primarykey, so same enames will be there ,so i need to rerieve datas where ename=a particular name and workid for that must be maximum.... please help me thanks ...

Chain together multiple complex WHERE clauses in LINQ to SQL

This is the pseudo-SQL I want to generate: SELECT * FROM Table WHERE Column1 = @Value1 OR Column2 = @Value2 The problem is, sometimes the second one should not be included. I was hoping to chain together .Where() clauses like so: var query = context.TableName; query = query.Where(t => t.Column1 == value1); if (NeedsToBeIncluded(valu...