linq-to-sql

Linq2Sql referring to entity column by variable creation

Hi, I have a linq2sql class with fields WeekEnding1 WeekEnding2 WeekEnding3 WeekEnding4 I want to write some c# using the fields in a for loop. Take this for example: for(int i=1; i<=4; i++) { Msgbox(myClass.WeekEnding + i) } I realise that wont work but what will?? Malcolm ...

LINQ TO SQL - Bulk Upate

Generally for updating an item i am using public static void UpdateCustomer<T>(T item) where T : class { var DB = GetNewDataContext(); var table = DB.GetTable<T>(); table.Attach(item); DB.Refresh(RefreshMode.KeepCurrentValues, item); DB.SubmitChanges(); } What is the way to update an...

Linq to SQL or the Entity Framework for Enterprise ASP.NET MVC application architecture?

I am still trying to figure out the right architecture for a complex ASP.NET MVC web application. I looked in a lot of example code and everywhere it's done differently. I would really appreciate your thoughts on this. Another Question: Would you use Linq to SQL or the Entity Framework? Thanks, -Ben ...

What type should I use in Database and Objects using Linq-to-SQL for floating point?

I have 2 fields and I need these to be a floating decimal point. I wonder what data type to use while I'm designing bottom-up Linq-to-SQL classes. EG: one value is 1.427600, another value is 1765.030, both would be in the same property/column. ...

Linq To Sql : add value transformation

Consider the following expression: from p in db.People select new Person { Name = p.Name, Age = p.Age, Gender = p.Gender.ToEnum<Gender>() }; It works to the point of calling extension method static T ToEnum<T>(this string value); as expected. I understand why there is do not know how to translate string to enum error. Qu...

Edit data with Linq to Sql in asp.net

I have an accounts table in my database (firstname, lastname, address, etc) and I need to display that on my asp.net page, BUT i need to enable editing also. I have tried using GridView, DetailsView etc, and none of them seem to enable editing? I am sure i am doing something wrong. I am using linqdatasource which connects to my reposit...

Linq to Sql Adding Two Records for a Single User

I am going nuts here! I am trying to figure out that why my UserRepository.Save method is inserting two records for the same user. Here is the code: [TestFixture] public class foo { private IUserRepository _userRepository; private IRoleRepository _roleRepository; private User _user; [SetUp] ...

Can you make Linq-to-SQL get rid of lazy loaded properties after they are saved?

I have an application that uses Linq-to-SQL and stores very large objects. When it's processing and saving these new objects, I want to keep them in memory, but after it saves I want to get rid of the lazy loaded property (the one that is taking up all the memory). Is there any way to do this without just getting rid of the object and r...

A little confused about where to put things in ASP.NET MVC

I'm working on my first ASP.NET MVC app, and I'm running into a little confusion about creating/updating certain data. I have a database table User, a LinqToSql-generated partial class User, and my own custom partial class User. I'm using [Bind(Exclude = "Id, InsertDateTime, UpdateDateTime")] on my version of User because I don't want ...

LINQ to SQL performance with "SELECT TOP {x}" queries

In looking up how to perform an equivalent to SELECT TOP 5 with LINQ-to-SQL, all the answers I've seen suggest using .Take(), like so: var myObject = ( from myObjects in repository.GetAllMyObjects() select myObject) .Take(10); I don't yet understand most of how LINQ works behind-the-scenes but to my understanding of C-like languages t...

columns not shown when binding to gridview using linq to sql with an empty datasource

Is there a way to display the columns of the data you are selecting from when binding to a datagrid with an empty datasource? Whenever I bind with an empty datasource, the grid won't even show. var results = from t in db.vwTaskInfos where t.PriorityId == Convert.ToInt32(drdPriority.SelectedValue) select t; gvTasks.DataSource ...

SQL metal to dbml, how to generate correct foreign key column names

I have this 2 tables table name: Person with Columns:PersonID, Name table name: VisitInfo with Columns: VisitPersonID , CoordinatorPersonID both columns have a foreign key to person table When i generate the code i get a VisitInfo class with the properties: Person CoordinatorPerson But what i want is a VisitInfoClass with the ...

Why isn't my value stored in the database with LINQ?

I'm using the following LINQ-statement to update a table-entry with the current date/time: MembershipClassesDataContext db = new MembershipClassesDataContext(); var tmp = db.drun_addqol_2_usrs.SingleOrDefault(y => !y.done.HasValue && y.UserId.Equals(Membership.GetUser().ProviderUserKey.ToString())); tmp.done = DateTime.Now; // at this p...

cannot add an entity with a key that is already in use

I have got this weird error 'cannot add an entity with a key that is already in use' But what is quite irritable about that error is that user gets no detais - who? what? what table? what record is the culprit of this error? It would be desperately complicated to determine it, in case you do many operations on LINQ objects before .Submi...

Given LINQ to Entities does not support "Custom methods" how do you stay DRY?

I have run into this problem: Custom Methods & Extension Methods cannot be translated into a store expression Basically I have some complicated LINQ queries, so wanted to break them down into subqueries which are implemented as methods that return IQueryables. My hope was then these IQueryables could be composed together in a LINQ stat...

How do I implement sorting for a custom return type from a linq-to-sql query?

I am using a repository class with linq-to-sql as the objectdatasource for a (web) GridView. The GridView has to allow sorting on all columns. I have a working solution using this approach but I would obviously prefer to do this without a predefined list of sort expressions. public class TrailerMovementRepository { private TrailerMo...

LINQ 2 SQL Group by multiple custom columns

Hi there. Is there a way to create LINQ query that will have different grouping fields. I.E. I have a class public class Stat { public DateTime Date { get; set; } public int ApplicationId { get; set; } public int ActionType { get; set; } public string Version { get; set; } } Now I need to query table with t...

How do I correctly separate the database operations from the gui/logic in c# when working with datasources?

Im having a really hard time figuring out how to specify a good search term for my problem: separation of gui and database interaction in visual studio 2008 using Linq to sql. According to my teacher in a c# class im taking it's not correct to let the GUI be dependant on a specific way of getting data. The way my project is currently s...

How to regenerate (update) LINQ to SQL DataContext?

I use LINQ to SQL in a WPF project. I have made small changes in the database (changed datatype of the fields, which contain no data). Now I want to regenerate (refresh) the LINQ to SQL DataContext of this project (I use VS 2008). I try to right-click on .dbml item in my Solution Explorer and choose "Refresh", but nothing happens. How ...

Recursive Selecting Similar Objects (LINQ)

I've got a bit of a situation with similar objects - basically, every object implements a collection of a base type. So ... Item1 - List Items2 - List Items3 public List Specials { get; set; } Item2 : Item1 Item3 : Item1 Special { public int Value { get; set; } public string Name { get; set; } } Now, I can just go down the tree ...