linq

Slow foreach() on a LINQ query - ToList() boosts performance immensely - why is this?

I kind of grasp the whole delayed execution concept, but the following has me puzzled... On a DataTable containing about 1000 rows, I call AsEnumerable(). I then select the entities returned into an IEnumerable of strongly typed classes (1)... Here's where I get confused: I do a foreach loop on the collection; selecting stuff from the i...

Linq to return ALL pairs of elements from two lists?

Given lists l1 = {1, 2} and l2 = {4, 5, 6 } I want to get a new list that has elements: rez = { {1, 4}, {1, 5}, {1, 6}, {2, 4}, {2, 5}, {2, 6} } Suggestions? ...

Displaying Hierarchical Data in Nested Repeaters

I have a entity in my database that looks like this; Item { ID, Text, Description, ... ParentID } Where ParentID references another Item.ID; I want to list these Items hierarchically using ASP.net. I assume I want to use a asp:repeater to do the nesting, but maybe not. If there's a better option I'm all for it....

Find all classes with an attribute containing a specific property value

Is it possible to find a class that is tagged with a custom attribute based on a value given to that attribute? Basically, I have classes that look like this - [MyAttr("CODE")] public class MyClass() {} From there I'm getting all the classes (Types) - var c = Assembly.GetExecutingAssembly().GetTypes().Where (...

LINQ no longer has intellisense in views

Ever since we upgraded to .Net 4.0 and VS2010 (from 3.5 and 2008) we can no longer get intellisense on LINQ to kick in when working in our views. Before the upgrade everything was working fine, but after the upgrade it's actually listed as an error when we try to use things like .Where() on a collection. Oddly it will compile fine and ru...

LinqServerModeDataSource devexpress

Hello, I have LinqServerModeDataSource as a datasource for my grid. It works great when I use it with tables from my database but when I want to make any custom changes it doesnt work correctly for example in onSelecting event: var Qry = from s in myContext select new { s.UserId, S.UserFirstName, s.UserLastName, Name = s.UserLastName...

Finding items that corresponds to other items in the same collection using LINQ

I'm trying to wrap my head around this in LINQPAD and keep failing. Basically I need the entries that have a corresponding entry ending with "_SCHEMA". So from the list below, I need "Dumbo" entry only void Main() { var users = new List<User> {new User{Name="Dummy"}, new User{Name="Dumbo"}, new User{Name="Dunno"}, new User{Name="Dumbo...

How to use distinct with group by in Linq to SQL

I'm trying to convert the following sql to Linq 2 SQL: select groupId, count(distinct(userId)) from processroundissueinstance group by groupId Here is my code: var q = from i in ProcessRoundIssueInstance group i by i.GroupID into g select new { Key = g.Key, Count = g.Select(x => x.UserID).Distinct().Count...

LINQ Query with dynamic number of boolean ANDs

I am working on a search web page for my site. The requirements state that users can enter text for any combination of 9+ fields and the search should do an 'AND' match when querying the database. I could fairly quickly write this as a stored procedure using 'ISNULL' but I'm trying to figure out how to accomplish the same thing in LINQ...

database actions select , update , insert and delete

Hi All: How i can make a decision to use Linq or Entity Framework as data access layer and business logic layer from performance viewpoint (execution time , roundtrips ). ...

Databind ListView datasource and joined tables

Hi all. I am not expert in ASP so i would be appreciated for the help. Issue connected to Entity FW, ListView Control, its Datasource and type convertion. I have the following LINQ query : RoutesEntities routesModel = new RoutesEntities(); LocalesEntities localesModel = new LocalesEntities(); ObjectQuery routesQuery = (ObjectQuery) f...

Entity SQL query with hierarchal relation

I want to use Entity SQL to query the elements of some subtype in my Entity Model. For instance... SELECT VALUE c FROM Persons AS c WHERE c is of (Customer) no problem meanwhile, but if I try the following query where Active is a property of Customer entity... SELECT VALUE c FROM Persons AS c WHERE c is of (Customer) AND c.Active == ...

Custom column in LinqServerModeDataSource

Hello, I have table in my database names User with fields: Id FirstName LasteName LoginName Now I would like to present this table in ASPxGridView using LinqServerModeDataSource. What I did is : <dxdtlnq:LinqServerModeDataSource ID="LinqServerModeDataSource1" runat="server" OnSelecting="LinqServerModeDataSource1_OnSelecting...

LINQ performance

I am reading records from database and check some conditions and store in List<Result>. Result is a class. Then performing LINQ query in List<Result> like grouping, counting etc. So there may be chance that min 50,000 records in List<Result>, so in this whether its better to go for LINQ (or) reinsert the records to db and perform the que...

LINQ - Does the Where expression return new instance or reference to object instance.

This is probably a basic question for some, but it affects how I design a piece of my program. I have a single collection of type A: IEnumerable<A> myCollection; I am filtering my collection on 2 different criteria: IEnumerable<A> subCollection1 = myCollection.Where(x => x.Count > 10); etc. Now, I know that the .Where expression w...

XLinq: Remove certain XElements from a xml file which are saved in a LIst<XElement>

Hello, I can not remove nodes while I iterate them thats ok.´ I have a List with Guid`s in it. I want to delete all XElements in that xml file where the XElement has a Guid of that List thats my xml file: <?xml version="1.0" encoding="utf-8"?> <Departments> <Department Id="2d55ba71-a2ab-44a1-a697-f57bbd238c7f" /> <Department Id=...

Xml.Linq - diffrences between XNA 4.0 (windows emulator), .NET 3.5 (Application)

I have created application for testing Linq queries (time) which I using in game project. And now, one query take 1.2s on Windows Application, but when I'm trying to use that same query on windows game (XNA 4.0) it is really, really, really slow and i don't know why. I cannot use SQLite database ( windows game is only for testing purpo...

Return @@RowCount in LINQ to SQL INSERT

I am in the process of migrating some legacy code to LINQ to SQL and I am having some difficulty with the following Stored Procedure. BEGIN INSERT INTO tblCMOEncounterHeader ( submitter_organization_id, submission_date, begin_posting_date, end_posting_date, number_of_records_transmitted) ...

Using LINQ calling a sproc, how can I pass a var back from a method?

I have this method which worked for a while public string getSlotText(int slotID) { DataClasses1DataContext context = new DataClasses1DataContext(); var slotString = context.spGetSlotTextBySlotID(slotID); return slotString.ElementAt(0).slotText; } But what i really want now is something like public var getSlotText(int ...

Optimizing simple usage of Linq in C#

Hi, I have replicated a stripped-down version of my code that has recently been re-written to use linq to access the database. However, in my opinion, the linq is really simple and could probably be optimized quite a bit, especially around line 90 where there is a linq statement inside a foreach loop. It'd be really helpful to see how...