Getting Nth value with Linq
var nthItem = items.Skip(n).First();
var nthItem = items.Skip(n).First();
Since you want to see if search contains a word which is contained in the description of p you basically need to test for each value in search if it is contained in the description of p result = from p in products where search.Any(val => p.Description.Contains(val)) select p; This is c# syntax for the … Read more
Excerpts from ECMA bible, verse 334 : 12.2 Default values The default value of a variable depends on the type of the variable and is determined as follows: For a variable of a value-type, the default value is the same as the value computed by the value-type’s default constructor (ยง11.1.1). For a variable of a … Read more
If you express it as a where clause it may just work out of the box with LINQ to SQL, if you can construct an appropriate expression. There may be a better way of doing this in terms of the expression trees – Marc Gravell may well be able to improve it – but it’s … Read more
Just add the orderby clause ๐ from a in Audits join u in Users on a.UserId equals u.UserId group a by a.UserId into g let score = g.Sum(x => x.Score) orderby score descending select new { UserId = g.Key, Score = score };
You need to reference System.Linq (e.g. using System.Linq) then you can do var dupes = dupList.GroupBy(x => new {x.checkThis, x.checkThat}) .Where(x => x.Skip(1).Any()); This will give you groups with all the duplicates The test for duplicates would then be var hasDupes = dupList.GroupBy(x => new {x.checkThis, x.checkThat}) .Where(x => x.Skip(1).Any()).Any(); or even call ToList() or … Read more
Anthony Pegram has said it the best. Use the right tool for the job. I say this because a Distinct or HashSet isn’t that big different when it comes to performance. Use a HashSet when the collection should always hold only distinct stuffs. It also tells the programmer that you cant add duplicates to it. … Read more
Yes it is indeed possible to pause execution midway through a linq query. Convert your linq to query style using lambda expressions and insert a Select statement that returns itself somewhere after the point in the linq that you want to debug. Some sample code will make it clearer – var query = dataset.Tables[0].AsEnumerable() .Where … Read more
Concat is the LINQ equivalent of UNION ALL in SQL. I’ve set up a simple example in LINQPad to demonstrate how to use Union and Concat. If you don’t have LINQPad, get it. In order to be able to see different results for these set operations, the first and second sets of data must have … Read more
Like this: var result = GetMyIEnumerable() .ToList(); result.ForEach(x => x.property1 = 100);