linq-to-sql

Conditional Linq Queries

时光毁灭记忆、已成空白 提交于 2020-02-08 19:24:26
问题 We're working on a Log Viewer. The use will have the option to filter by user, severity, etc. In the Sql days I'd add to the query string, but I want to do it with Linq. How can I conditionally add where-clauses? 回答1: if you want to only filter if certain criteria is passed, do something like this var logs = from log in context.Logs select log; if (filterBySeverity) logs = logs.Where(p => p.Severity == severity); if (filterByUser) logs = logs.Where(p => p.User == user); Doing so this way will

Updating GridView Data using LINQ

纵然是瞬间 提交于 2020-02-05 04:07:04
问题 I have a GridView which needs to get updated on RowUpdating Event. How can I update data using LINQ? I am populating GV using LINQ too and here is the code for that: protected void Page_Load(object sender, EventArgs e) { string getEntity = Request.QueryString["EntityID"]; int getIntEntity = Int16.Parse(getEntity); using (OISLinq2SqlVs1DataContext dt = new OISLinq2SqlVs1DataContext()) { var tr = from r in dt.Users join s in dt.Entities on r.Entity_ID equals s.ID where s.ID == getIntEntity

Creating static Datacontext() or Creating whenever its needed. Which is Better, and Why?

不想你离开。 提交于 2020-02-04 01:05:51
问题 I have a function, and inside that I am creating a DataContext() everytime the function is called. What is the cost of creating a new DataContext() . Can I create a static DataContext() and use it everywhere. Because The DataContext has a full recording of all the changes when SubmitChanges() fails , is there a way I can remove those specific changes from the DataContext when SubmitChanges() fails. My Question is which is better Creating static Datacontext() or Creating whenever its needed ?

Creating static Datacontext() or Creating whenever its needed. Which is Better, and Why?

风流意气都作罢 提交于 2020-02-04 01:04:05
问题 I have a function, and inside that I am creating a DataContext() everytime the function is called. What is the cost of creating a new DataContext() . Can I create a static DataContext() and use it everywhere. Because The DataContext has a full recording of all the changes when SubmitChanges() fails , is there a way I can remove those specific changes from the DataContext when SubmitChanges() fails. My Question is which is better Creating static Datacontext() or Creating whenever its needed ?

linq to sql ExecuteQuery() as IQueryable

北慕城南 提交于 2020-02-03 09:16:48
问题 ExecuteQuery() method returns an IEnumerable but is there a way to make it return IQueryable? 回答1: Well, you can call AsQueryable , but it won't do any good. The problem is that when you use ExecuteQuery , the query isn't composable because LINQ to SQL doesn't "understand" it as such. One of the core purposes of IQueryable<T> is to allow the various aspects of a query to be composed together and then LINQ to SQL can convert them into a single SQL query. That just doesn't work when one of the

linq to sql ExecuteQuery() as IQueryable

牧云@^-^@ 提交于 2020-02-03 09:16:40
问题 ExecuteQuery() method returns an IEnumerable but is there a way to make it return IQueryable? 回答1: Well, you can call AsQueryable , but it won't do any good. The problem is that when you use ExecuteQuery , the query isn't composable because LINQ to SQL doesn't "understand" it as such. One of the core purposes of IQueryable<T> is to allow the various aspects of a query to be composed together and then LINQ to SQL can convert them into a single SQL query. That just doesn't work when one of the

LINQ many-to-many relationships: Solution?

ぃ、小莉子 提交于 2020-02-03 05:17:32
问题 LINQ so far has been remarkably elegant, but to perform basic m2m queries it offers no solution I can imediately see. What's worse is that while it works for any other table relationship, LINQ is not giving me an association in the class structure for my m2m table. So I can do things like artwork.artists.where(...) //or artist.Artworks.add(artwork) but I can't do artwork.artowrks_subjects.tagSubjects.where(...) //or tagSubject.artworks_subjects.add(artwork) alt text http://img299.imageshack

LINQ where condition with dynamic column

一曲冷凌霜 提交于 2020-01-30 06:56:05
问题 I have this code // IQueryable<General> query if (columnName == "Column1") { query = query.Where(x => x.Column1 == searchValue); } else if (columnName == "Column2") { query = query.Where(x => x.Column2 == searchValue); } else if (columnName == "Column3") { query = query.Where(x => x.Column3 == searchValue); } else if (columnName == "Column4") { query = query.Where(x => x.Column4 == searchValue); } // next zilions columns to come // ... and my question is. How can i past x.Column as a

Why is TransactionScope using a distributed transaction when I am only using LinqToSql and Ado.Net

廉价感情. 提交于 2020-01-30 06:02:08
问题 We are having problems on one machine, with the error message: "MSDTC on server XXX is unavailable." The code is using a TransactionScope to wrap some LingToSql database code; there is also some raw Ado.net inside of the transaction. As only a single sql database (2005) is being accessed, why is a distributed transaction being used at all? (I don’t wish to know how to enable MSDTC, as the code needs to work on the server with their current setup) 回答1: This almost always happens when your

Why is TransactionScope using a distributed transaction when I am only using LinqToSql and Ado.Net

限于喜欢 提交于 2020-01-30 06:01:39
问题 We are having problems on one machine, with the error message: "MSDTC on server XXX is unavailable." The code is using a TransactionScope to wrap some LingToSql database code; there is also some raw Ado.net inside of the transaction. As only a single sql database (2005) is being accessed, why is a distributed transaction being used at all? (I don’t wish to know how to enable MSDTC, as the code needs to work on the server with their current setup) 回答1: This almost always happens when your