SingleOrDefault: How to change the default values?
you can do something like myStrings.DefaultIfEmpty(“myDefaultString”).Single() check out here
you can do something like myStrings.DefaultIfEmpty(“myDefaultString”).Single() check out here
IronPython 2.7 finally bridges this gap with the clr.ImportExtensions method which adds the extension methods from a namespace to the target types e.g. >& ‘C:\Program Files\IronPython 2.7\ipy.exe’ IronPython 2.7 (2.7.0.40) on .NET 4.0.30319.225 Type “help”, “copyright”, “credits” or “license” for more information. >>> import clr >>> clr.AddReference(“System.Core”) >>> from System.Collections.Generic import List >>> dir (List) … Read more
var query = someList.Where(a => (someCondition)? a == “something” : true); so, if ‘someCondition’ is false, ‘Where’ will be skipped.
You could load the data and sort in memory after loading it. IEnumerable<Team> teams = _ctx.Teams .Include(x => x.TeamMembers) .Include(x => x.TeamMembers.Select(u => u.User)) .Where(x => x.UserId == ownerUserId) .OrderBy(x => x.Name).ToList(); foreach (var team in teams) { team.TeamMembers = team.TeamMembers.OrderBy(m => m.Name); foreach (var teamMember in team.TeamMembers) { teamMember.Users = teamMember.Users.OrderBy(u => u.Name); } … Read more
You can do this, even across servers, as long as you can access one database from the other. That is, if it’s possible to write a SQL statement against ServerA.DatabaseA that accesses ServerB.DatabaseB.schema.TableWhatever, then you can do the same thing in LINQ. To do it, you’ll need to edit the .dbml file by hand. You … Read more
You can use the PredicateBuilder class: var searchPredicate = PredicateBuilder.False<Songs>(); foreach(string str in strArray) { var closureVariable = str; // See the link below for the reason searchPredicate = searchPredicate.Or(SongsVar => SongsVar.Tags.Contains(closureVariable)); } var allSongMatches = db.Songs.Where(searchPredicate); LinqToSql strange behaviour
Do you want to add it to the database, or just to the list of results that other things like databinding will use? If it’s the former, you’ll need to use the normal add operation for whatever kind of LINQ you’re using, but to the table representation rather than the IQueryable. Queries only read data. … Read more
It’s really identical to map from functional languages. The reason it’s named Select is that it’s designed to be used as a part of LINQ which uses SQL-like keywords. from item in collection where item.Value == someValue select item.Name is translated to: collection.Where(item => item.Value == someValue) .Select(item => item.Name) it would be a little … Read more
I have done this join so many times that I could not figure out why it would not work until I found a post from Justin Niessner here that says “The names of the properties in the anonymous types (as well as their types) must match exactly.” That lead me to this code: using(var db … Read more
Taken from 101 LINQ Samples: int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int[] numbersB = { 1, 3, 5, 7, 8 }; IEnumerable<int> aOnlyNumbers = numbersA.Except(numbersB); Console.WriteLine(“Numbers in first array but not second array:”); foreach (var n in aOnlyNumbers) { Console.WriteLine(n); } Result Numbers in first array but not second … Read more