Can Func's point to instance methods ?
Whats the difference between Func and Delegates ?
Difference between lock and Thread.QueueWorkUserItem
Can delegates point to instance methods ?
-----
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqExamples
{
#region Data
class Product
{
public string Name { get; set; }
public int CategoryID { get; set; }
}
class Category
{
public string Name { get; set; }
public int ID { get; set; }
}
public class Student
{
public string Name { get; set; }
public string Standard { get; set; }
public string Status { get; set; }
public Student(string name, string standard, string status)
{
this.Name = name;
this.Standard = standard;
this.Status = status;
}
}
#endregion
class Program
{
static void Main(string[] args)
{
/*
var users = new List<User>
{
new User{Id = 1, FirstName = "Sam"},
new User{Id = 2, FirstName = "Dave"},
new User{Id = 3, FirstName = "Julia"}
};
var groups = new List<Group>
{
new Group{Id = 1, Name = "NJHawks"},
new Group{Id = 2, Name = "NYJets"}
};
var userGroups = new List<UserGroup>
{
new UserGroup{UserId = 1, GroupId=1},
new UserGroup{UserId = 1, GroupId=2},
new UserGroup{UserId = 2, GroupId=2},
new UserGroup{UserId = 3, GroupId=1},
new UserGroup{UserId = 3, GroupId=2}
};
var innerJoinQuery = from u in users
join ug in userGroups on u.Id equals ug.UserId
join g in groups on ug.GroupId equals g.Id
where u.FirstName == "Sam"
select new {UserId = u.Id, u.FirstName, GroupName = g.Name};
foreach (var item in innerJoinQuery)
{
Console.WriteLine("FirstName : {0} ,UserId : {1}, GroupId : {2} ", item.UserId, item.FirstName, item.GroupName);
}
*/
//Updating Linq Objects
/*var arrStudents = new Student[]
{
new Student("AAA","1","A"),
new Student("BBB","2","B"),
new Student("CCC","3","C"),
new Student("DDD","4","D"),
new Student("EEE","5","E"),
new Student("FFF","6","F"),
new Student("GGG","7","G"),
new Student("HHH","8","H"),
new Student("III","9","I")
};
Console.WriteLine("Before update");
foreach (Student student in arrStudents)
{
Console.WriteLine(String.Format("{0},{1},{2}", student.Name, student.Standard, student.Status));
}
var query = (from stud in arrStudents
where stud.Name == "BBB"
select stud)
.Update(st => { st.Standard = "0"; st.Status = "X"; });
Console.WriteLine("After update");
foreach (Student _student in arrStudents)
{
Console.WriteLine(String.Format("{0},{1},{2}", _student.Name, _student.Standard, _student.Status));
}*/
/*Update Collections */
/*Create data repository*/
var fundFamilies = new List<FundFamily>
{
new FundFamily {Id = 1, Code = "RE"},
new FundFamily {Id = 2, Code = "NON-RE"},
new FundFamily {Id = 3, Code = "PEQ"}
};
var funds = new List<Fund>
{
new Fund {Id = 1, Code = "MSREF1", FundFamilyId = 1},
new Fund {Id = 2, Code = "MSREF2", FundFamilyId = 1},
new Fund {Id = 3, Code = "EQ1", FundFamilyId = 2}
};
var modelDatas = new List<ModelData>
{
new ModelData()
{ModelCode = "BSP", ModelName = "Boston Seaport", FundCode = "MSREF1"},
new ModelData()
{ModelCode = "WBS", ModelName = "Woodbrook Sea", FundCode = "MSREF1"},
new ModelData()
{ModelCode = "EQTL", ModelName = "Private Tryport", FundCode = "EQ1"},
new ModelData()
{ModelCode = "EQTL1", ModelName = "Private Tryport2", FundCode = "EQ1"}
};
var query = from f in funds
join ff in fundFamilies on f.FundFamilyId equals ff.Id
select new {FundFamilyId = ff.Id, FundFamilyCode = ff.Code, Fundid = f.Id, FundCode = f.Code};
//foreach (var bp in query)
// Console.WriteLine("Funds info - {0}, {1}, {2}, {3}", bp.FundFamilyId, bp.FundFamilyCode, bp.Fundid, bp.FundCode) ;
//Create ModelTransaction
var modelTransactions = new List<ModelTransaction>
{
new ModelTransaction()
{ModelCode = "BSP", ModelName = "Boston Seaport", FundCode = "MSREF1"},
new ModelTransaction()
{ModelCode = "WBS", ModelName = "Woodbrook Sea", FundCode = "MSREF1"},
new ModelTransaction()
{ModelCode = "EQTL", ModelName = "Private Tryport", FundCode = "EQ1"},
new ModelTransaction()
{ModelCode = "EQTL1", ModelName = "Private Tryport2", FundCode = "EQ1"}
};
Console.WriteLine("--Before Update----");
foreach (ModelTransaction modelTransaction in modelTransactions)
Console.WriteLine("Funds info - {0}, {1}, {2}, {3}", modelTransaction.FundFamilyId,
modelTransaction.FundCode, modelTransaction.FundId, modelTransaction.FundFamilyId);
Console.WriteLine("--After Update----");
var metaDataQuery = (from f in funds
join ff in fundFamilies on f.FundFamilyId equals ff.Id
select new {FundCode = f.Code, f.FundFamilyId, FundId = f.Id});
//.Update(mt => { mt.FundFamilyId = 1 ; mt.FundId = 2; });
//var updateModelTransactionsQuery = (
// from m in modelTransactions
// join f in funds on m.FundCode equals f.Code
// join ff in fundFamilies on f.FundFamilyId equals ff.Id
// select m);
//foreach (var m in updateModelTransactionsQuery)
//{
//}
foreach (ModelTransaction modelTransaction in modelTransactions)
{
int fundId= metaDataQuery.FirstOrDefault(i => i.FundCode == modelTransaction.FundCode).FundId;
int fundFamilyId= metaDataQuery.FirstOrDefault(i => i.FundCode == modelTransaction.FundCode).FundFamilyId;
modelTransaction.FundId = fundId;
modelTransaction.FundFamilyId = fundFamilyId;
}
foreach (ModelTransaction modelTransaction in modelTransactions)
Console.WriteLine("Funds info - {0}, {1}, {2}, {3}", modelTransaction.FundFamilyId, modelTransaction.FundCode, modelTransaction.FundId, modelTransaction.FundFamilyId);
Console.ReadLine();
}
}
public static class UpdateExtensions
{
public delegate void Func<TArg0>(TArg0 element);
public static int Update<TSource>(this IEnumerable<TSource> source, Func<TSource> update)
{
if (source == null) throw new ArgumentNullException("source");
if (update == null) throw new ArgumentNullException("update");
if (typeof(TSource).IsValueType)
throw new NotSupportedException("value type elements are not supported by update.");
int count = 0;
foreach (TSource element in source)
{
update(element);
count++;
}
return count;
}
}
}
Whats the difference between Func and Delegates ?
Difference between lock and Thread.QueueWorkUserItem
Can delegates point to instance methods ?
-----
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqExamples
{
#region Data
class Product
{
public string Name { get; set; }
public int CategoryID { get; set; }
}
class Category
{
public string Name { get; set; }
public int ID { get; set; }
}
public class Student
{
public string Name { get; set; }
public string Standard { get; set; }
public string Status { get; set; }
public Student(string name, string standard, string status)
{
this.Name = name;
this.Standard = standard;
this.Status = status;
}
}
#endregion
class Program
{
static void Main(string[] args)
{
/*
var users = new List<User>
{
new User{Id = 1, FirstName = "Sam"},
new User{Id = 2, FirstName = "Dave"},
new User{Id = 3, FirstName = "Julia"}
};
var groups = new List<Group>
{
new Group{Id = 1, Name = "NJHawks"},
new Group{Id = 2, Name = "NYJets"}
};
var userGroups = new List<UserGroup>
{
new UserGroup{UserId = 1, GroupId=1},
new UserGroup{UserId = 1, GroupId=2},
new UserGroup{UserId = 2, GroupId=2},
new UserGroup{UserId = 3, GroupId=1},
new UserGroup{UserId = 3, GroupId=2}
};
var innerJoinQuery = from u in users
join ug in userGroups on u.Id equals ug.UserId
join g in groups on ug.GroupId equals g.Id
where u.FirstName == "Sam"
select new {UserId = u.Id, u.FirstName, GroupName = g.Name};
foreach (var item in innerJoinQuery)
{
Console.WriteLine("FirstName : {0} ,UserId : {1}, GroupId : {2} ", item.UserId, item.FirstName, item.GroupName);
}
*/
//Updating Linq Objects
/*var arrStudents = new Student[]
{
new Student("AAA","1","A"),
new Student("BBB","2","B"),
new Student("CCC","3","C"),
new Student("DDD","4","D"),
new Student("EEE","5","E"),
new Student("FFF","6","F"),
new Student("GGG","7","G"),
new Student("HHH","8","H"),
new Student("III","9","I")
};
Console.WriteLine("Before update");
foreach (Student student in arrStudents)
{
Console.WriteLine(String.Format("{0},{1},{2}", student.Name, student.Standard, student.Status));
}
var query = (from stud in arrStudents
where stud.Name == "BBB"
select stud)
.Update(st => { st.Standard = "0"; st.Status = "X"; });
Console.WriteLine("After update");
foreach (Student _student in arrStudents)
{
Console.WriteLine(String.Format("{0},{1},{2}", _student.Name, _student.Standard, _student.Status));
}*/
/*Update Collections */
/*Create data repository*/
var fundFamilies = new List<FundFamily>
{
new FundFamily {Id = 1, Code = "RE"},
new FundFamily {Id = 2, Code = "NON-RE"},
new FundFamily {Id = 3, Code = "PEQ"}
};
var funds = new List<Fund>
{
new Fund {Id = 1, Code = "MSREF1", FundFamilyId = 1},
new Fund {Id = 2, Code = "MSREF2", FundFamilyId = 1},
new Fund {Id = 3, Code = "EQ1", FundFamilyId = 2}
};
var modelDatas = new List<ModelData>
{
new ModelData()
{ModelCode = "BSP", ModelName = "Boston Seaport", FundCode = "MSREF1"},
new ModelData()
{ModelCode = "WBS", ModelName = "Woodbrook Sea", FundCode = "MSREF1"},
new ModelData()
{ModelCode = "EQTL", ModelName = "Private Tryport", FundCode = "EQ1"},
new ModelData()
{ModelCode = "EQTL1", ModelName = "Private Tryport2", FundCode = "EQ1"}
};
var query = from f in funds
join ff in fundFamilies on f.FundFamilyId equals ff.Id
select new {FundFamilyId = ff.Id, FundFamilyCode = ff.Code, Fundid = f.Id, FundCode = f.Code};
//foreach (var bp in query)
// Console.WriteLine("Funds info - {0}, {1}, {2}, {3}", bp.FundFamilyId, bp.FundFamilyCode, bp.Fundid, bp.FundCode) ;
//Create ModelTransaction
var modelTransactions = new List<ModelTransaction>
{
new ModelTransaction()
{ModelCode = "BSP", ModelName = "Boston Seaport", FundCode = "MSREF1"},
new ModelTransaction()
{ModelCode = "WBS", ModelName = "Woodbrook Sea", FundCode = "MSREF1"},
new ModelTransaction()
{ModelCode = "EQTL", ModelName = "Private Tryport", FundCode = "EQ1"},
new ModelTransaction()
{ModelCode = "EQTL1", ModelName = "Private Tryport2", FundCode = "EQ1"}
};
Console.WriteLine("--Before Update----");
foreach (ModelTransaction modelTransaction in modelTransactions)
Console.WriteLine("Funds info - {0}, {1}, {2}, {3}", modelTransaction.FundFamilyId,
modelTransaction.FundCode, modelTransaction.FundId, modelTransaction.FundFamilyId);
Console.WriteLine("--After Update----");
var metaDataQuery = (from f in funds
join ff in fundFamilies on f.FundFamilyId equals ff.Id
select new {FundCode = f.Code, f.FundFamilyId, FundId = f.Id});
//.Update(mt => { mt.FundFamilyId = 1 ; mt.FundId = 2; });
//var updateModelTransactionsQuery = (
// from m in modelTransactions
// join f in funds on m.FundCode equals f.Code
// join ff in fundFamilies on f.FundFamilyId equals ff.Id
// select m);
//foreach (var m in updateModelTransactionsQuery)
//{
//}
foreach (ModelTransaction modelTransaction in modelTransactions)
{
int fundId= metaDataQuery.FirstOrDefault(i => i.FundCode == modelTransaction.FundCode).FundId;
int fundFamilyId= metaDataQuery.FirstOrDefault(i => i.FundCode == modelTransaction.FundCode).FundFamilyId;
modelTransaction.FundId = fundId;
modelTransaction.FundFamilyId = fundFamilyId;
}
foreach (ModelTransaction modelTransaction in modelTransactions)
Console.WriteLine("Funds info - {0}, {1}, {2}, {3}", modelTransaction.FundFamilyId, modelTransaction.FundCode, modelTransaction.FundId, modelTransaction.FundFamilyId);
Console.ReadLine();
}
}
public static class UpdateExtensions
{
public delegate void Func<TArg0>(TArg0 element);
public static int Update<TSource>(this IEnumerable<TSource> source, Func<TSource> update)
{
if (source == null) throw new ArgumentNullException("source");
if (update == null) throw new ArgumentNullException("update");
if (typeof(TSource).IsValueType)
throw new NotSupportedException("value type elements are not supported by update.");
int count = 0;
foreach (TSource element in source)
{
update(element);
count++;
}
return count;
}
}
}
No comments:
Post a Comment