ffrom p in products join c in categories on p.CategoryId equals c.Id into g from c in g.DefaultIfEmpty() select new { CategoryTitle = c == null ? string.Empty : c.Title, ProductTitle = p.Title };
Показ дописів із міткою linq. Показати всі дописи
Показ дописів із міткою linq. Показати всі дописи
вівторок, 8 листопада 2011 р.
LINQ: left join
вівторок, 5 квітня 2011 р.
View LINQ Generated SQL
Write class:
Use:
VS output:
public class LinqDebugger : System.IO.TextWriter
{
static readonly LinqDebugger _instance = new LinqDebugger();
static readonly string _separator = new String('_', 150);
public static LinqDebugger Instance { get { return _instance; } }
private LinqDebugger()
{
}
public override void WriteLine(string value)
{
System.Diagnostics.Debug.WriteLine(value);
if (!string.IsNullOrEmpty(value) && value.StartsWith("-- Context:"))
System.Diagnostics.Debug.WriteLine(_separator);
}
public override System.Text.Encoding Encoding
{
get { throw new NotImplementedException(); }
}
}
Use:
var db = new DBDataContext();
db.Log = LinqDebugger.Instance;
VS output:

понеділок, 21 лютого 2011 р.
Linq Paging Extensions
public static class PagingExtensions
{
//linq to sql
public static IQueryablePage (this IQueryable source, int page, int size)
{
return source.Skip((page - 1) * size).Take(size);
}
//linq to objects
public static IEnumerablePage (this IEnumerable source, int page, int size)
{
return source.Skip((page - 1) * size).Take(size);
}
}
вівторок, 28 вересня 2010 р.
Select Top n Random Items
class Item
{
public string Name { get; set; }
}
...
var list = new List- {
new Item() { Name="Item1" },
new Item() { Name="Item2" },
new Item() { Name="Item3" },
new Item() { Name="Item4" },
new Item() { Name="Item5" },
};
Random rnd = new Random();
var items = from i in list
orderby rnd.Next()
select i;
var top = items.Take(3);
Підписатися на:
Дописи (Atom)