linq-to-sql

How convert sql query to linq-to-sql

I have Sql query: SELECT News.NewsId, News.Subject, Cm.Cnt FROM dbo.News LEFT JOIN (SELECT Comments.OwnerId, COUNT(Comments.OwnerId) as Cnt FROM Comments WHERE Comments.CommentType = 'News' Group By Comments.OwnerId) Cm ON Cm.OwnerId = News.NewsId But I want linq-to-sql query, how I can convert this to linq? ...

Which fieldtype is best for storing PRICE values?

Hi there I am wondering whats the best "price field" in SQL Server for a shoplike structure? Looking at this overview: http://www.teratrax.com/sql_guide/data_types/sql_server_data_types.html We have data types called money, smallmoney, then we have decimal/numeric and lastly float and real Name, memory/disk-usage and value ranges: M...

How can i export records of more than 1,00,000 from sql server database to CSV formats in LINQ?

My team working asp.net File manage project In our project we need to export database contents to csv formats The Database table contains 6 fields We are using LINQ. ...

Foreach loop problem for Iqueyable object

Hi, Can we use foreach loop for Iqueryable object? I'd like to do something as follow: query = Iqueryable<Myclass> = objDataContext.Myclass; // objDataContext is an object of LINQ datacontext class int[] arr1 = new int[] { 3, 4, 5 }; foreach (int i in arr1) { query = query.Where(q => (q.f_id1 == i || q.f_id2 == i || q.f_id3 == i)); ...

How to get the affected rows number of Linq To Sql?

How to get the affected rows number of Linq To Sql? I use Linq To Sql to delete a batch of records in the sql server 2005. How can I get the affected rows number? And How can I know how many records have been deleted if that is something wrong happened in the batch delete? ...

How can I make this LinqToSQL query work? (SqlExecption)

The following code gives me an SqlException: Invalid object name 'dbo.studentsCourses' OO theCourse = subject.Course; var students = dc.studentsCourses.Where(x => x.course == theCourse).Select(x => x.student); I tried the following code instead but I also get an Exception. My original question was asked on Aardvark and can be read be...

Linq to Sql get SqlCommand when stored procedure execution fails

Currently I'm assigning a TextWriter to the Log property of my Linq to Sql data context (per request instancing) and write this to my logging when an exception is thrown while executing a stored procedure (is strongly typed mapped in the context, so not executing a custom command) however when using ADO.NET we normally inspect the SqlCo...

How to check if a child-object is populated

How can i check if a child-object of a linq-object is populated or not? Example code below. My model have two methods, one joins data, and the other does not: public static Member GetMemberWithPhoto(Guid memberId) { using (DataContext db = new DataContext()) { DataLoadOptions dataLoadOptions = new DataLoadOptions(); ...

Connecting to SQL Server Express in silverlight 4 appl, the db doesn't shows up in Management Studio Express

I'm using SQL Server Express named instance and LinqToSql in my Silverlight 4 application. The database located in the web application App_Data folder. I attached the db via VS2010. The connection string generated by the program automatically, after double clicked on the db file. When I load the solution, I get an error: "The connecti...

Composite primary keys in N-M relation or not?

Lets say we have 3 tables (actually I have 2 at the moment, but this example might illustrate the thought better): [Person] ID: int, primary key Name: nvarchar(xx) [Group] ID: int, primary key Name: nvarchar(xx) [Role] ID: int, primary key Name: nvarchar(xx) [PersonGroupRole] Person_ID: int, PRIMARY COMPOSITE OR NOT? Group...

Linq-to-sql join/where?

I've the following table structures Users id Types id isBool UsersTypes userid types I want to select all the UserTypes based on id and isBool. I tried this query var q = from usertype in usertypes from type in types where type.isBool == false where userstypes.user == id select usertype; But thi...

Linq 2 SQL Grouping Question

var groups = from p in dc.Pool join pm in dc.PoolMembers on p.ID equals pm.PoolID group p by p.Group into grp select new { grp.ID }; This isn't working. Basically I want to do the grouping, and then select certain columns, but when I do select new { grp. } I get no intellisense, so I'm obviously ...

Oddities in Linq-to-SQL generated code related to property change/changing events

I'm working on creating my own Linq-to-Sql generated classes in order to learn the concepts behind it all. I have some questions, if anyone knows the answer to one or more of these I'd be much obliged. The code below, and thus the questions, are from looking at code generated by creating a .DBML file in the Visual Studio 2010 designer,...

Use LINQ to SQL results inside SQL Server stored procedure

Note: I'm not trying to call a SQL Server stored proc using a L2SQL datacontext. I use LINQPad for some fairly complex "reporting" that takes L2SQL output saved to an Array and is processed further. For example, it's usually much easier to do multiple levels of grouping with LINQ to Objects instead of trying to optimize a T-SQL quer...

Databinding multiple tables linq query to gridview?

My question is how can I display a linq query in a gridview that has data from multiple tables AND allow the user to edit some of the fields or delete the data from a single table? I'd like to do this with either a linqdatasource or a linq query. I'm aware I can set the e.Result to the query on the selecting event. I've also been able t...

How to SET ARITHABORT ON for connections in Linq To SQL

By default, the SQL connection option ARITHABORT is OFF for OLEDB connections, which I assume Linq To SQL is using. However I need it to be ON. The reason is that my DB contains some indexed views, and any insert/update/delete operations against tables that are part of an indexed view fail if the connection does not have ARITHABORT ON. E...

Several SubmitChanges in succession very slow

Hi, One question: Why when I use following code for (int i = 0; i < 10000; i++) { Entity e = new Entity(); e.DisplayValue = i.ToString(); ctx.Entities.InsertOnSubmit(e); } ctx.SubmitChanges(); it finishes after about 8 seconds but when I use this code for (int i = 0; i < 10000; i++) { Entity e =...

How to create Linq2Sql query, calculating aggregates?

Is there any way to create Linq2SQL query, that will be translated to this: SELECT COUNT(*) as totalCount , SUM(v.field1) AS totalfield1, .... SUM(v.<fieldN>) AS total<fieldN> FROM [dbo].[SomeTable] v ...

How to: Display multiple related classes in an ASP.NET GridView ?

I would like to display students and their grades with a GridView and LinqToSQL like this: assignment1 assignment2 Student 1 55 89 Student 2 87 56 Student 3 92 ...

what is the syntax in linq to insert a record using ObjectSet<> instead of using the .AddToXXXXX(MyEntity) as this is deprecated?

I am new to Linq to Entities and I am trying to insert a record using the linq syntax. I have created the edmx file and instatiated it in a class with: PasswordEntities db = new PasswordEntities(); I have a method that looks like this: public void InsertRecord(Password record) { db.AddToPasswords(record); }...