linq-to-sql

How can I connect to a database on a remote server in a background thread?

I have a windows form, on which I display data from a local database. I'd like to also connect to a remote database, and display some other data from there.. however, this remote database could be down, or slow. I don't want the UI to freeze while I try to connect to this remote database. So knowing nothing about threading or thread-...

How to config. Linq DataContext (in Class Lib Proj) to use connection string in web.config of web application?

I'm looking for a solution on how to configure Linq DataContext in Class Library Project in order to use Connection String in web.config of web application. Your advice would be much appreciated. ...

Linq To Sql - Update a value when another value changes

With Linq To Sql - what is the best way to update the value of one table in a database when a value changes in a different table? For example in TableA, there is a column called DateModified. TableA has an association to TableB. Now I want to set the value of the DateModified field to the current date every time the record changes in ta...

Linq's OnValidate event doesn't get fired

I validate data for a table made in the linq designer in the event OnValidate. This event is fired when I insert records, but not is fired when I update records. I have this code: public bool Save(int id, string marca, string modelo, string año, string motor, bool disponible, RuleList issues) { Usado u; if (id == 0) { ...

LINQ: Remove items from IQueryable

I want to remove an item from the result of a LINQ query before using it to databind. What is the proper way to do this? The foreach in my illustration is the topic of my question. Illustration: var obj = (from a in dc.Activities where a.Referrer != null && a.Referrer.Trim().Length > 12 && a.Session.IP.NumProblems == 0...

HtmlEncode string within IQueryable without altering bound data

I'm binding an ASP.NET control to the result of a LINQ query. I'd like to HtmlEncode one of the properties of the contained objects before binding to the control, but I want to do it without altering the data because I do a DataContext.SubmitChanges() later on. How can this be done? Code that won't work: var ds = (from s in dc.Search...

Return plain objects in entity framework for serialization

I have been trying out both Linq to Sql and EF in my ASP.NET MVC application. After switching to EF I realized my XML/JSON serialization output has extra cruft. XML: <Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; <EntityKey> <EntitySetName>Persons</EntitySetName> <...

This IQueryable<T> extension method has no supported translation to SQL :(

Hi folks, I'm trying to do the following linq 2 sql extension method: public static PagedList<T> ToPagedListOrNull<T>(this IQueryable<T> value, int index, int pageSize) { return value.Count() == 0 ? null : (value ...

To return IQueryable<T> or not return IQueryable<T>

I have a repository class that wraps my LINQ to SQL Data Context. The repository class is a business line class that contains all the data tier logic (and caching and such). Here's my v1 of my repo interface. public interface ILocationRepository { IList<Location> FindAll(); IList<Location> FindForState(State state); IList<L...

Using value from related table in LINQ where clause

I was expecting the following LINQ query to retrieve all contacts with the specified phone number but instead it returns all contacts that don't have a phone number at all. var query = from contact in dc.Contacts where contact.Phones.All(phone => phone.PhoneNumber == "5558675309") select contact; What am I doin...

LINQ and .COUNT timing out

I have a general question, and curiosity about LINQ and timeouts. I have a live application running and I am receiving timeouts on the following code. The following code, which is normal, and I don't see anything wrong: private static tblUser GetUserLinq(string email, string password) { DataContext db = new DataCon...

LINQ to SQL partial class OnValidate Changeaction.Delete

OK here's my problem... I have a LINQ partial class: public partial class resp { public IEnumerable<RuleViolation> GetRuleViolations() { if (String.IsNullOrEmpty(respName)) yield return new RuleViolation("Responsibility name required", "respName"); yield break; } public bool IsValid { ...

Best way to wire up database-driven menu in ASP.NET MVC

I'm looking for a way to handle a database-driven menu in ASP.NET MVC that doesn't violate the MVC principles. I want to replace the hard-coded, default, "Home, About" menu with something from my database. How would I wire this up? Would I just set up a ContentPlaceHolder in my Site.Master and have regenerated in my Views? That does...

How to show numerical status in MVC application

I am building a new application based on MVC and considering LINQ-To-SQL/Entity Framework. I a have entity which has a numerical status Like (0 - Open, 1 - Active, 2 - Closed), Where should i Put the logic to show appropriate string (Enum??) in the view/controller or model which is generated automatically? Should I move all such logic in...

Constrain LINQ2SQL Datacontext to specific SQL Application Role

In SQL Server you are able to have application role security, through which you are able to for example give specific permissions that originate from specific applications. You can execute sp_SetAppRole() to set the application role but was wondering how this could be accomplished when using a LINQ2SQL datacontext with the least amount ...

How to insert only new records using Linq-to-SQL?

I have to insert some data periodically in my SQL Server database. But the feeds where I read the data repeats some data that was inserted before. When I use Linq-to-SQL to insert into the DB either some data is duplicated, or a primary key violation exception is raised, depending on the primary key. How to insert the data without dupli...

How to setup a property from 1 linq2sql obj that is populated by a relationship to another obj

I'm using Linq2SQL and I'm pretty new to it. I've got a User table and a UserData table, the idea being that properties for the User object can be added / removed by adding or removing rows in the UserData table. I did not come up with this particular design but I am more or less stuck with it (as long as I can come up with a solution)...

N-Tiered LinqToSql Question

Hello. I am hoping you can help. I am developing a tiered website using Linq to Sql. I created a new class(or object) in DBML designer called memberState. This object is not an actual table in the database. I have this method in my middle layer: public override IEnumerable(memberState) GetMembersByState(string @state) { using (Bulle...

How to compare values from different tables with a single query on Linq-to-SQL?

The code, with two Linq-to-SQL queries, that I am trying to optimize is below: var maxAInstant = ( from a in db.As select a.Instant ) .Max(); var maxBInstant = ( from b in db.Bs select b.instant ) ...

TransactionScope, linq and strange transaction manager issue (HRESULT: 0x8004D024)

I have a service level methods, which make few changes to database and I want them to use transaction control. Such methods can do following: - LINQ SubmitChanges() functionality - Calls to StoredProcedures Component users can combine set of such elementary operations into something bigger. I see that there is nice class TransactinSco...