linq

quick linq-to-sql what would be more efficient.

What is the life cycle of a data context. MyDB_DataContext db = new MyDB_DataContext(); Is it more efficient to pass one context across several methods or create new instances of it. OR is there any reason to choose one over the other public void DoStuff(){ MyDB_DataContext db = new MyDB_DataContext(); doMoreStuff() } private ...

Foreach loop through ObservableCollection

In WPF app I use LINQ query to get values from ObservableCollection and update the state of some objects: var shava = from clc in _ShAvaQuCollection where clc.xyID == "x02y02" select clc; switch (shava.First().Status) { case 1: x02y02.Status = MyCustControl.Status.First; break...

IList query in Linq in MVC application

Hello All, I want this below class to return IList<> Please tell me why it is not working public IList<CheckBoxListInfo> GetLinks() { string linkName = string.Empty; int linkId = 0; using (var db = new brandconnectionsEntities()) { var query = from s in db.BC_TabTa...

Is it possible to change the pluralized name of a LINQ table?

For example, my table name is "Diagnosis". When I bring it into my LINQ to SQL model, it tries to figure out how to pluralize it, and it mistakenly assumes the entity name should be "Diagnosi" and the set name should be "Diagnosis", which could be confusing. When I change the entity name to "Diagnosis" in the properties, it doesn't chan...

LINQ Query using Group By

I have a different answers associated with particulary QuestionID.I have to group the answers of each question. Question Table ---------- QuestionID QuizID QuestionText IsMultipleAnswers Answer Table -------- AnswerID QuestionID AnswerText IsCorrectAnswer var query = from qst in context.Questions join ans in context.An...

LINQ method with varying parameters

I have a LINQ method for the Search page of an in house app. The method looks as below public static DataTable SearchForPerson(String FirstName, String MiddleName, String LastName, String SSN, DateTime? BirthDate) { var persons = (from person in context.tblPersons where person.LastName == LastNam...

How to prevent double round trip with Linq and ToArray() Method

I am trying to use an Array instead of a list in my query. But I must get the count first before I can iterate through the objects returned from the database. Here is my code: var FavArray = favorites.OrderByDescending(y => y.post_date).Skip((page - 1) * config.MaxRowsPerPage).Take(config.MaxRowsPerPage).ToArray(); int F...

Linq to build hierarchical list

I have a method that retrieves a set of records from a database. I map this data to a business object - let's call the business object ProcessModel. One of the properties in ProcessModel is a list, called ProcessChildren. The property is List. So, the data is linked by various fields/properties. There is one object at the top of the...

Check if there's a open conection to database asp.net/c#

Everytime my application runs a storedprocedure it does something like this: using (DbBase conn = new DbBase()) { //call sproc } the DBBase() opens the connection with a DataContext from LINQ. What I wanted to know, if there's a way to know if a connection has already been open, and use that instead of opening a new one. That v...

Convert string to linq query

Hello Everyone - I was playing around with LINQPad and was curious how I could implement similar behavior in my own app, namely: how can I allow the user to input a LINQ query against a known database context as a string and then run that query in the application? For example, if I have the LINQ-to-SQL datacontext for the Northwind dat...

Replacing a regular method with an anonymous method in C#/LINQ

I have a LINQ query that looks like this: public IEnumerable<Foo> SelectFooBars() { return from f in foos join b in bars on f.BarId equals b.Id select AddMissingProp(f, b.MissingProp); } public void AddMissingProp(Foo foo, string missingProp) // substitute this...

Finding a control on a Winforms using Linq?

I am trying to find an elegant way to get controls on a Winforms by name. For example: MyForm.GetControl "MyTextBox" ... But this has to make sure it goes through all the controls recursively. What's the most elegant way to implement this using Linq? ...

SQL Server RowVersion

Hey guys, Quick question Re: Implementation of RowVersion. I am looking to include it in my next database (Designing it now) and I am pondering 2 different ways: RowVersion row in all tables A separate RowVersion table which has the ID of the table as a foreign key and linked to it. Anyone tried this? If so what pitfalls or pro's d...

Extract sql query from LINQ expressions.

Is it possible to extract sql statements from LINQ queries ? Say, I have this LINQ expression. string[] names = new string[] { "Jon Skeet", "Marc Gravell", "tvanfosson", "cletus", "Greg Hewgill", "JaredPar" }; var results = from name in names where name.Star...

Converting generic collection sort to utilize Linq

I have an existing bit of code for sorting a list of objects. productsList.Sort( delegate(Product p1, Product p2) { int result = p1.StartDatetime.CompareTo(p2.StartDatetime); if (result == 0) { if (p1.SomeId == p2.SomeId) { result = 0; } else if ...

How to do the following in Dynamic Linq using System.Linq.Dynamic namespace

Hi, I just want to know how to do the following in Dynamic Linq using System.Linq.Dynamic namespace var query2 = db.Customers.Select( cust => new Customer { FirstName = cust.FirstName, LastName = cust.LastName Tags="sampleTag...

Linq: join the results in a IEnumerable<string > to a single string

Hi, how do i join the results in a IEnumerable to a single string? the IEnumerable contains 20 single letters, and i want it to combine it to a single string. And out of curiousity: how would i join it with a separator, for example if the IEnumerable contains the strings a b c d e how can i join it to a,b,c,d,e? Michel ...

C#/Linq combobox databinding

I'm working on a project where I'm using DataBindings to update certain fields based on currently selected field. I have a series of textBoxes which are databound to a combo box, which at present is currently linked to a table as a data source. However there is a relationship between the ID of two tables, meaning the combobox should be l...

C# XML Serialization with numerous elements of data

I have some XML that I am trying serialize like so: string Value = @"<MSG> <ABCID> <ID>0123456789A</ID> <ID>0123456790B</ID> </ABCID> <DATETIME>2010-01-07T13:00:09</DATETIME> ...

Refering to other entities when using Linq2Sql and Sprocs

We are evaluation Linq2Sql for internal applications, and our development guidelines mean that we must always use stored procedures for all CRUD operations, from various blogs i have got together an application that does much of what we want. However, what i would like to do is when we have a relationship between two entities the relati...