linq

Why is using an object property in LINQ slower than using a primitive?

Why does this LINQ query (Id is a property of type long in the Structure object): IList<Structure> theStructures = new List<Structure>(); public int GetChildrenSlow(Structure aStructure){ IEnumerable<Structure> childrenQuery = from structure in theStructures where structure.ParentStru...

Sorting a List<int>

Using C# what is the best way to sort a List numerically? my list has items 5,7,3 and I would like them sorted 3,5,7. I know some longer ways, but I would imagine linq has a quicker way? sorry this was end of day, my mind is else where it worked, didn't see it change the first time:( ...

Foreaching through grouped linq results is incredibly slow, any tips?

I did some profiling on a program that I'm running and the thing that takes the longest time is getting the results from the linq query: var Results = from a in Table group a by a.Value into b select new {Group = b}; foreach(var Result in Results) { //Do calcs } Any ideas on how I can speed this up? ...

Linq-to-sql Not Contains or Not in?

I'm building a poll widget. I've 2 tables, call them Polls and PollsCompleted. I need to do a linq query to get all the Polls that do not exist for a given user in PollsCompleted. I have the following sets: For Polls Where Active == True For PollsCompleted Where UserId == ThisUserId Where PollId = Polls.Id Now I need to get all Polls...

What is the difference between ((IEnumerable)source).OfType<T>() and source as IEnumerable<T>

What is the difference between ((IEnumerable)source).OfType<T>() and source as IEnumerable<T> For me they look similar, but they are not! source is of type IEnumerable<T>, but it is boxed as an object. Edit Here is some Code: public class PagedList<T> : List<T>, IPagedList { public PagedList(object source, int index, int pageSiz...

How to use Expression to build an Anonymous Type?

In C# 3.0 you can use Expression to create a class with the following syntax: var exp = Expression.New(typeof(MyClass)); var lambda = LambdaExpression.Lambda(exp); object myObj = lambda.Compile().DynamicInvoke(); But how do you use Expression to create an Anonymous class? //anonymousType = typeof(new{ Name="abc", Num=123}); Type anon...

C# string[]ToList then ToString() with Delimeter

Possible Duplicate: What is the LINQ way to implode/join a string array? Say I have a linq query that returns a generic list containing a single string element: //Actually comes from a linq to sql query so I don't actually have the array. string[] mer = {"cat","dog","fish"}; var k = (from k in mer select k); Is the...

MySQL timestamp to .NET timestamp

Hi all, I am using DBLinq to access a MySQL DB from C# .NET 3.5 I have created the dbml file for the Visual Studio 2010 ORM, and it loads fine - however when I compile the source I am met with errors regarding the conversion of MySQL timestamp to .NET DateTime. Now this kind of makes sense, because they are 2 'different' types (Though...

Parsing a text File using Linq

Hi all I have a log file in the format below as you can see each log start with a time and ends with pipe delimeter. Put each Log starting with a dateTime and ending with a pipe delimeter in a List How can I parse this text file and put the logs in the collections? I seem to have a problem in determine how can I find the start and ...

Can 't create extesion method for LINQ to Enitities link DeleteAllOnSubmit() in LINQ to SQL

Since there is no DeleteAllOnSubmit() method for LINQ to Entities. So I myself create it and named for DeleteAllObjecst as following code. public static void DeleleAllObjects(this ObjectSet objectSet, TEntity[] objects) { foreach(var o in objects) { objectSet.DeleteObject(o); } } However the ...

How do I use linq to do a WHERE against a collection object (using Netflix data source)

Hello I am using LinqPad to learn Linq by querying the NetFlix OData source. (BTW I know their is a similar question already on SO...didn't help me). Here is the query I got working which is awesome. from x in Titles //where x.Rating=="PG" where x.Instant.Available==true where x.AverageRating>=4.0 //where x.Rating.StartsWith("TV")...

Website database duplicate records

On every page of my website a token is passed in as a querystring parameter. The server-side code checks to see if the token already exists in the database. (The token is a uniqueidentifider field in the database). If the token exists then it will use the existing one, if not then it will create a new row with the new token. The problem...

fastest way to see if IEnumerable<T> contains item based on criteria

Sort of a Linq beginners question, but is there a simple built-in way to optimize this: bool containsItemWithValue42 = items.Where(i => i.Value == 42).Count() > 0; I would like Linq to stop iterating as soon as it found a match. ...

Simple check to see if at least one object in a set has a property value of TRUE

I have a set of model objects that have a public IsVisible boolean property. All I need to do is find if at least one of the set has that value set to TRUE. In other words, if I have 10,000 objects but the second one is true, I don't need to spin through the other 9,998. I already have my answer. Now I know I could write my own itera...

Can I do a "Take(x)" on the "Include" entity in a Linq Query?

Hi folks, i've got a really simple linq to entites statement :- var query = (from q in Users.Include("UserHistory") select q).Take(20); works great ... except that, for each user .. the history can be n+1. Some users have 100's of UserHistory records. So, can I restrict the the number of UserHistory records to .. 10 or 5...

how to return a value from a LINQ statement

var x = from y in db.table where y.member_id == 1 select new { y.member_id }; this statement should return only one record. & i want to retrieve that value the statement returns in a string format. like string m = x; how can i do that?! ...

LINQ to Object comparing two lists of integer for different values

I accept both C# and VB.NET suggestion, even though I'm writing an app in VB.NET I have two lists of intergers List1 {1,2,3,5} List2 {2,4,6,7} I want to have new List3 {4,6,7} which is composed of elements of List2 that are not in List1. I know I can write a nice For Each loop for this but I want it done in LINQ I've been lookin...

Pivot DataTable with Unknow number of columns

How would you go about pivoting data in a datatable where the number of columns varies from? A few columns would always be in the datatable such as ID and Name but the rest could vary. I have been struggling a lot with this and can't seem to come up with an elegant solution. Does anyone have a suggestion? ...

LINQ InvalidOperationException when making a query

Ok, last edit, promise: With the following class: public partial class MembershipModule : BaseConnection<MembershipModule> { private const string AccessPrivilege = "Access"; public bool Accessible() { return this.Privileges().Any(p => p.Name.Equals(AccessPrivilege)); } public IQueryable<MembershipAction> P...

LINQ2SQL LIKE Command for Phrase

Hi all, I am trying to retrieve a list of string where it contains "Britney Spears", and this is what I use from p in Objects where p.Title.Contains("Britney Spears") select p That works fine, but if I want to select title which is "Britney Jean Spears", "Britney 'Sexy' Spears" it doesn't work, so my question is how do I insert a wil...