linq

Speed up LINQ inserts

Hi, I have a CSV file and I have to insert it into a SQL Server database. Is there a way to speed up the LINQ inserts? I've created a simple Repository method to save a record: public void SaveOffer(Offer offer) { Offer dbOffer = this.db.Offers.SingleOrDefault ( o => o.offer_id == offer.offer_id); ...

Linq and Binary Search - Improve this slow Where statement?

I've got two collections, each containing about 40,000 items. The elements in list 2 are linked to the elements of list one via a foreign key. For each element of list one, I want to find the corresponding element in list two. Something like this: foreach(var item in list1) { var match = list2.Where(child => child.ID == item.ChildI...

Entity Framework - dynamic sql

I'm implementing a search feature for an app that uses entity framework. There are several optional fields for searching a particular database table/view. What is the best way to implement such a search with EF? Stored procedure? Or can it be done (realistically) using Linq only? ...

Complex Expressions in a LINQ Where Clause

I was wondering if it is possible to include inner variables or delegates in linq statements? I currently am using Linq to XML with XPath extensions and am using a where clause on an element that I cannot guarantee will exist. Here is a sample of what I mean: var result = from record in xml.Root.XPathSelectElements("record") w...

In a multitier application should a client be allowed to send its own linq expressions to the server?

The rationale: HQL and NH criteria are NHibernate specific constructs and as such they are server side DAL implementation details. I do not want them to "leak" to the client side. So, our client side provides LINQ expressions for the server to process. Seems legitimate to me, some, however, think otherwise and so I would like to know ...

Replace nested ForEach with Select if applicable

Is it possible to replace method ForEach() usage with Select() or something else to write next code in one string with nested extension methods? OR maybe there are another ways to improve the algorithm? var list = new List<IStatementParser>(); System.IO.Directory.GetFiles(path, "*.dll") .ForEach(f => System.Reflection.Assembly.Load...

NHibernate.Linq And CompareTo String

I'm using Linq and Hibernate and trying to compare to Strings one from a variable and the other from a Class Linked to Hibernate, the code: bindingSource.DataSource = (from search in Repository.GetAll() where search.cod_coluna.CompareTo(CurrentRecord.cod_coluna) > 0 orderb...

Reading XML element & child nodes using LINQ in Vb.net- help me in where condition :(

Hi, I am new in LINQ world. I need an urgent help in reading the xml elements using LINQ with specific where condition. I need to find the max air_temp for a county i.e where county name = "Boone" and hour id = "06/03/2009 09:00CDT" i tried something like below, but no luck : Dim custs As IEnumerable = From c In Element.Load("C:\me...

split generic list

I need to split a list into two equal lists. For Example: I have a list which consists of 10 items. I need to split the list into two equal parts(each with 5 items) I have a list which consists of 9 items sometimes. I need to split the list into two parts(one with 5 items and other with 4 items) Please suggest a solution for this. ...

having and conditional count() in linq query

I want to create this query: select Something, count(Something) as "Num_Of_Times" from tbl_results group by Something having count(Something)>5 I started with this: tempResults.GroupBy(dataRow => dataRow.Field<string>("Something")) .Count() //(.......what comes here , to make Count()>5?) ...

Find next record in a set: LINQ

I have a list of objects which all have an id property E.g 1, 10, 25, 30, 4 I have a currentId and I need to find the next Id in the list So for example current Id is set to 25, I need to return the object with an id of 30. The one after that would be 4. How would I do this elegantly in LINQ? EDIT The list is ordered by a "sort" p...

Linq/XML: grouping results properly within XML element - with inner joins!

In a previous question I asked about how to group XML elements logically, and I got the answer, which was to nest the Linq query. Problem is, this has the effect of left-joining the nested queries. For example, let's say I want to list all the cities in the USA that begin with the letter "Y", grouped by State and County: XElement xml ...

VB.NET and LINQ. What am I missing here?

I'm trying to get this sitemap class working. It appears to use LINQ, which I've never used, but half the fun of programming is learning new stuff! My problem is that I'm getting compile errors where the LINQ code is. VS just doesn't recognize it. I've a reference to system.data.linq, I've an imports system.data.linq, but still where...

why the sql query is different on that linq query when run on c# and on vb.net?

if I run this under c# from p in Addresses where p.Address2 == null select p.AddressID it generate this query SELECT [t0].[AddressID] FROM [dbo].[Address] AS [t0] WHERE [t0].[Address2] IS NULL if I run this under vb.net from p in Addresses where p.Address2 = nothing select p.AddressID it generate this query SELECT [t0].[Address...

How can i get items which are equal to string[] ?

Hi, I have an array of strings var controlsToGet = new[] {"lblHome","lblContact"}; I have List<LanguageControl> and LanguageControl class holds Controls in it. I want to get Controls from List which Control.Name == controlsToGet I am looking for something like that var all = fooelements.where(l=>l.Control.Name == controlsToGet); ...

How to add custom columns to a table that LINQ to SQL can translate to SQL

I have a table that contains procedure codes among other data (let's call it "MyData"). I have another table that contains valid procedure codes, their descriptions, and the dates on which those codes are valid. Every time I want to report on MyData and include the procedure description, I have to do a lookup similar to this: From m in ...

How can I make sure that FirstOrDefault<KeyValuePair> has returned a value

Here's a simplified version of what I'm trying to do: var days = new KeyValuePair<int, string>(); days.Add(1, "Monday"); days.Add(2, "Tuesday"); ... days.Add(7, "Sunday"); var sampleText = "My favorite day of the week is 'xyz'"; var day = days.FirstOrDefault(x => sampleText.Contains(x.Value)); Since 'xyz' is not present in the KeyVal...

Collection-valued parameters with The Entity Framework ?

Hi, In my last project i decided to use Entity Framework and it was all going well until i try to get datas with "where in", i got an error. After a tiny search i ve come up with this post and that post. This is what i am trying to do var all = fooelements .Where(l=>controlsToGet .Contains(l....

how to write parameter string for include for linq query?

If table Employee has foreign key for table person, department, when get the entity of employee, I want to both associated entities loaded too. how to write the linq query? like below? var employee = this.Context.Employee.Include("Person, Department"); It seems not working. ...

Replacing nested foreach with LINQ; modify and update a property deep within

Consider the requirement to change a data member on one or more properties of an object that is 5 or 6 levels deep. There are sub-collections that need to be iterated through to get to the property that needs inspection & modification. Here we're calling a method that cleans the street address of a Employee. Since we're changing data ...