linq-to-sql

Linq to sql multiple data context in same transaction

Hi Experts, I am working on a project Where I have multiple reposetories to fetch data from differnt tables. All my repositories are independent, they create new dataContext, Add row to table and apply submit changes command. Now if in my service, there is a situation where I have to insert data into multiple tables, but it should happ...

Parse string as DateTimeOffset in Expression<Func<T, bool>>

Hey, I am trying to create an Expression<Func<T, bool>> in which a string property is converted/cast to a DateTimeOffset so that DateTimeOffset operations can be performed on it. We are using Linq2SQL as our data provider, which does support converting strings to the SQL equivalent of DateTimeOffset. Ideally I want the expression to be...

Problems with automatic property creation when manually mapping relationships between tables with no foreign keys in L2S

Pardon the massive headline. I'm in the situation of having to build an application on top of a database, that I cannot make any changes to. The database does not have any primary- or foreignkeys set. I'm using linq-2-sql, and I'm interested in having some properties exposed on the entities generated from my dbml. For instance, in the ...

How can I clone (or copy) a object to another one, but don't copy PK attribute??

Hi! I'm trying to copy all object attibutes to another object, fox example: Person p1 = new Person(); p1.name = "John"; p1.sex = 'M'; Person p2 = new Person(); p2 = Util.Clone(p1); The problem is that Person entity has an identity PK 'codPerson' and I don't want to copy this PK. Is there a way to clone/copy an object, but don't copy ...

Get data from database between specified date and time range using linq-sql

I have a table with two separate columns for date and time (SQL Server 2008). I am trying to get the data between 1 minute before current time and 1 minute after current current time in my asp.net MVC (C#) application. I have tried to use the where condition as: ce.Start_Date.Add(ce.Start_Time) >= _currDateTime.AddMinutes(-1) && ce.S...

UDF - Return a table from a custom sql string execution with "Exec 'select * from ....' "

Hello everyone, How can I return a table from an UDF that executes a custom query string? Eg: CREATE FUNCTION fx_AdvancedSearch ( @keywords varchar(255), (..... other params ....) ) RETURNS TABLE AS BEGIN DECLARE @SqlQuery varchar(1000) ..... SET @SqlQuery = 'my custom select string previously generated' EX...

Refreshing BindingSource after insert (Linq To SQL)

I have a grid bound to a BindingSource which is bound to DataContext table, like this: myBindingSource.DataSource = myDataContext.MyTable; myGrid.DataSource = myBindingSource; I couldn't refresh BindingSource after insert. This didn't work: myDataContext.Refresh(RefreshMode.OverwriteCurrentValues, myBindingSource); myBindingSource.Re...

How do you save a Linq object if you don't have its data context?

I have a Linq object, and I want to make changes to it and save it, like so: public void DoSomething(MyClass obj) { obj.MyProperty = "Changed!"; MyDataContext dc = new MyDataContext(); dc.GetTable<MyClass>().Attach(dc, true); // throws exception dc.SubmitChanges(); } The exception is: System.InvalidOperationException: An enti...

LinqToSql returns null when using Single - not sequence contains no elements

Possible Duplicate: Linq help - Sql trace returns result, but datacontext returning null Question 1579164 I tried changing to .Single instead of .SingleOrDefault and the object I get back is null, not 'sequence contains no elements' error. When I use SQL profiler and capture the SQL that is generated, I run the query and get 1 ...

Refactoring Func<T> into Expression<Func<T>>

I have a method that currently takes a Func<Product, string> as a parameter, but I need it to be an Expression<Func<Product, string>>. Using AdventureWorks, here's an example of what I'd like to do using the Func. private static void DoSomethingWithFunc(Func<Product, string> myFunc) { using (AdventureWorksDataContext db = new Adven...

Linq to Sql - Convert C# to VB.NET help

I'm having trouble converting a C# Linq statement to VB.NET. I need the following statement converted. I am getting hung up on the i.Sum(v=>v) part. from p in Products from u in Users let i = (from op in OrderProducts where op.Order.User == u && op.Product == p select op.ProductQty) let quant = i.Count() == 0 ? 0 :...

Linq to SQL: how to aggregate without a group by?

I am searching for the Linq-to-SQL equivalent to this query: SELECT [cnt]=COUNT(*), [colB]=SUM(colB), [colC]=SUM(colC), [colD]=SUM(colD) FROM myTable This is an aggregate without a group by. I can't seem to find any way to do this, short of issuing four separate queries (one Count and three Sum). Any ideas? ...

How do I filter an entity based on another entity? Linq many-to-many in C#.

This is easy to do in SQL and I'm having a very hard time achieving this using Linq to SQL. I have two entities setup, Project and ProjectsbyUser: [Table(Name = "Projects")] public class Project { [Column(IsPrimaryKey = true, IsDbGenerated = true, Name="Job")] public string ProjectId { get; set; } [Column(Name = "Descript...

Selecting columns from a LINQ query

I want to be able to select the columns that are displayed in my DataGridView. I.e., I have a linq query that returns an IEnumerable but I don't want to display all the properties of Policy - I want to allow the user to choose what to display. So I thought something like this might work to create a "narrower" object with only the columns...

LINQ to SQL in ASP.NET MVC results in DuplicateKeyException

I'm following the Sports Store example in Pro ASP.NET MVC Framework and I'm getting an exception related to LINQ that I cannot figure out. The full code is available through the website but here is a snippet to convey the problem. private Table<Product> productsTable; // ... public void SaveProduct(Product product) { if (product.Prod...

What should be the best way to load setting from Database?

Hello, how should i load the table "Setting" into an asp.net mvc so that i can use it as a reference setting for the whole application. Is there anyway to save the memory and usage to do this problem? In my understanding, if i have settings in database, i will have to make the program load the table into a variable, then call out. But i...

Retrieving an Expression from a property and adding it to an expression tree

I've tried to simplify this example, as the actual code I'm playing with is more complex. So while this example may seem silly, bear with me. Let's say I'm working with the AdventureWorks database and I decide I want to add a property called Blarg to the Product table that returns an expression that contains code I would like to use in...

LINQ to SQL refactoring foreach help

Can this be refactored into one LINQ statement? I feel like it can be but can't wrap my head around it. The mishmash of extension methods and LINQ just looks ugly to me. (db is a DataContext.) void AddToSeries(Series series, DateTime date) { foreach (var date in db.Ad.Select(ad => ad.DateTime.Date).Distinct()) { var ph...

LinqToSql giving 'Specified cast is not valid'

Hi- I'm running a stored procedure through L2S and it's returning 'Specified cast is not valid'. The stored proc is returning data when ran manually and when I step thru it, everything is fine until it tries to create the row object in "foreach (var row in result)". var q = new db(); var result = q.GetNearbyLocations(latitude, longitude...

LINQ refactoring help needed

How would you refactor this code, with respect to the LINQ? I'm new to LINQ and haven't quite have a good handle on more complex queries (nested, grouping). Can all of these three statements and foreach loop been converted into one LINQ statement? void AddSeries(Series series, int phraseId) { using (var db = Database.Instance) ...