4. 联结操作符—【LINQ标准查询操作符】
public class Join_LINQ
{
static string ContextString = System.Configuration.ConfigurationSettings.AppSettings["ContextString"].ToString();
static DataContext context = new DataContext(ContextString);
static Table<Contact> contact = context.GetTable<Contact>();
static Table<Employee> emp = context.GetTable<Employee>();
#region Join
public static void Print_Join()
{
var joinQuery = from c in contact
join e in emp on c.ContactID equals e.ContactID
where c.FirstName.StartsWith("R")
orderby c.LastName
select new { e.EmployeeID, c.FirstName, c.LastName, c.EmailAddress, e.Title, e.HireDate };
// 方法语法
var joinMethodQuery = contact.Join(emp, c => c.ContactID, e => e.ContactID, (c, e) => new { e.EmployeeID, c.FirstName, c.LastName, c.EmailAddress, e.Title, e.HireDate }).Where(c => c.FirstName.StartsWith("R")).OrderBy(c => c.LastName);
foreach (var item in joinQuery.Take(3))
{
Console.WriteLine(item);
}
Console.ReadKey();
}
#endregion
#region GroupJoin
public static void Print_GroupJoin()
{
List<Foods> foods = new List<Foods> { new Foods { Kind = "Meat" }, new Foods { Kind = "Fruit" }, new Foods { Kind = "Drink" } };
List<Food> food = new List<Food> {
new Food{Name= "Beef",FoodKind="Meat"},
new Food{Name = "Chicken",FoodKind = "Meat"},
new Food{Name = "Apple",FoodKind = "Fruit"},
new Food{Name = "Pear",FoodKind = "Fruit"},
new Food{Name = "Coca Cola",FoodKind = "Drink"},
new Food{Name = "OX",FoodKind = "Drink"},
new Food{Name = "Beer",FoodKind = "Drink"},
};
var groupJoinQuery = foods.GroupJoin(food, fs => fs.Kind, f => f.FoodKind, (fs, f) => new { FoodKind = fs.Kind, FoodName = f.Select(o => o.Name) });
foreach (var item in groupJoinQuery)
{
Console.WriteLine(item.FoodKind);
foreach (var i in item.FoodName)
{
Console.WriteLine(" " + i.ToString());
}
}
Console.ReadKey();
}
#endregion
}
public struct Foods
{
public string Kind;
}
public struct Food
{
public string Name;
public string FoodKind;
}
【天天来(http://www.daydaycome.com)】- 精选折扣商品,爆料精选,九块九白菜底价尽在天天来!是一个中立的,致力于帮助广大网友买到更有性价比网购产品的分享平台,每天为网友们提供最受追捧 最具性价比 最大幅降价潮流新品资讯。我们的信息大部分来自于网友爆料,如果您发现了优质的产品或好的价格,不妨给我们爆料(谢绝商家)

浙公网安备 33010602011771号