『壹』 C#的问题,怎么在List集合中筛选数据
方法一:Linq
ChannelList就是一个List类型的数据,IsOpen是其元素的属性
channelCount=(fromchannelinDevicesManager.Instance.CurrentDevice.ChannelList
wherechannel.IsOpen
groupchannelbychannel.ChannelID).Count();
方法二:泛型委托Predicate<T>
publicdelegateboolPredicate<inT>(
Tobj
)
方法三、
///<summary>
///筛选运送方式
///</summary>
///<paramname="list">运送方式集合</param>
///<paramname="strType">运送方式</param>
///<returns></returns>
privateList<FeeRuleDto>selectList(List<FeeRuleDto>list,stringstrType)
{
returnlist.FindAll(delegate(FeeRuleDtoinfo){
if(info.DeliveryType.ToString()==strType)
{
returntrue;
}
else{
returnfalse;
}
});
}
方法四、
使用List<T>获取数据库表的时候,每次用户操作都重新访问数据库,然后返回List<T>,会严重影响程序运行效率的方式,其实List<T>自带有筛选的方法,把想要的数据筛选到另一个List<T>中,不用重新访问数据库,直接筛选后羡模绑定控件显示即可。
示例如下:
publicNumberModelcurrentmark;
publicMainFrmmainFrm;
privateList<宏派茄GoodsModel>goodslist;
privateList<GoodsKindModel>goodskindlist;
privatevoidlstgoodkind_SelectedIndexChanged(objectsender,EventArgse)
{
try
{
if(lstgoodkind.SelectedValue.ToString()!="XY.Model.GoodsKindModel")
{
stringid=lstgoodkind.SelectedValue.ToString();
stringkname=lstgoodkind.Text;
if(kname!="全部")
{
List<GoodsModel>glist=goodslist.FindAll(delegate(GoodsModelp){returnp.GoodsKind==kname;});蔽察
bindgoods(dgvgoods,glist);
}
else
{
bindgoods(dgvgoods,goodslist);
}
}
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
privatevoidbtnAdd_Click(objectsender,EventArgse)
{
try
{
stringgid=dgvgoods.Rows[dgvgoo
例如:跳过List前50条,然后取100条,可写为:
iclist_temp、iclist都为List类型
iclist_temp = iclist.Skip(50).Take(100).ToList();
取前100条,可以写为:
iclist_temp = iclist.Take(100).ToList();
ds.SelectedRows[0].Index].Cells["goodsid"].Value.ToString();
GoodsModelgoods=goodslist.Find(delegate(GoodsModelp){returnp.ID==gid;});
XY.BLL.ConsumeBll.Add(goods,currentmark,mainFrm.user);
}
catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}
}
『贰』 242泛型集合Dictionary<K,V>的使用
1、Dictionary<K,V>泛型集合
Dictionary<K,V>通常称为字典,<K,V>约束集合中元素类型
编译时检查类型约束, 无需拆箱装箱操作,与哈希表类似
2、Dictionary<K,V>的存储结构
对比: List对于无序念乎对象更有优势
3、Dictionary<K,V>的应用
数据帆族查找:
数据遍历:只能遍历键或者值,不态高弊能同时遍历
『叁』 C#怎么定义泛型集合类型的函数,返回一个集合
list<int> num()
{.....
......
return num;}
『肆』 c#泛型集合的遍历
有三种
很简单,给你举个例子:
1、先声明一个亮雹Dictoinary<key,value>泛型集合
创建一个Student类的对象Student stu=new Student()(在这个类中
有一个name属性)
Dictionary<student.name,Student> students=new Dictionary<student.name,Student>();
students.Add(stu);
2、开始遍历
(1)可以用value遍历就是Stuent的 对象
foreach(Student stu in students.values)
{}
(2)也可以用key遍历 即student.name
foreach(string key in students.keys)
{}
3、另外list<T>泛型集合也同样的道理,只不过,
它遍历时要通过索引
例如梁指
list<Student> stu=new list<Student>();
stu(1).name;
希望你能明白橡键配
『伍』 c#泛型 练习题!!
1.实现员工考勤信息管理,A:实现新增员工(窗体中有工号,年龄,姓名,性别【下拉框】)B:使用dataGridview控斗瞎件展示员工信息,在datagridview总使用右键删除,添加,在此控件上方有文本框和一查询按钮,根空毁空据员工工号查询 要求:使用泛型集合list的添加,查询和删除操作,使用泛型集合绑定dataGridview
2.实现员工打卡(签到,签退,在第一题的datagridview的窗体中右键操作再加上签到,签退标签) A:每天只能签到一次B:签退前必须已经签到C:显示打卡记录 要求:泛型集余岁合Dictionary的基本操作,使用泛型集合绑定dataGridview
『陆』 求泛型集合的增删改查的代码(c#)
public abstract class BaseDAL<T>:IBaseDAL<T> where T:class
{
net11Entities db = new net11Entities();
/// <summary>
/// 查询
/// </summary>
/// <returns></returns>
public List<T> Select()
{
return db.Set<T>().ToList();
}
/// <summary>
/// 添加
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int Add(T m)
{
db.Set<T>().Add(m);
return db.SaveChanges();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="m"></param>
/// <returns></returns>
public int Del(Expression<Func<T,bool>> where)
{
try
{
//DBContextFactory.GetDbContext();
var s= db.Set<T>().Where(where).FirstOrDefault();
db.Set<T>().Remove(s);
//db.Set<T>().Remove(m);
return db.SaveChanges();
}
catch(Exception se)
{
return 0;
}
}
/// <summary>
/// 修改
/// <和派/summary>
/// <param name="m">唤睁贺</param>
/// <returns></returns>
public int Upt(T m)
{
db.Set<T>().Attach(m);
db.Entry(m).State = System.Data.EntityState.Modified;
return db.SaveChanges();
}
}
不知道是不是你想要的。。。早梁。
『柒』 c#中BindingSource与泛型集合绑定数据后怎么筛选筛选条件是类里面的id属性
只有实现 IBindingListView 接口的基础列表才支持筛选
你用肆高的裂陆尺泛型悉宽集合不支持.
『捌』 有关泛型
可以的,下面是说明。
<T> where T : 类型约束
T:结构
类型参数旦老洞必须是值类型。可以指定除 Nullable 以外的任何值类型含姿。有关更多信息,请参见使用可空类型(C# 编程指南)。
T:类
类型参数必须是引用类型,包括任何类、接口、委托或数组类型。
T:new()
类型参数必须具有无参数模枯的公共构造函数。当与其他约束一起使用时,new() 约束必须最后指定。
T:<基类名>
类型参数必须是指定的基类或派生自指定的基类。
T:<接口名称>
类型参数必须是指定的接口或实现指定的接口。可以指定多个接口约束。约束接口也可以是泛型的。
T:U
为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。这称为裸类型约束。
msdn中的例子:
public class Employee
{
private string name;
private int id;
public Employee(string s, int i)
{
name = s;
id = i;
}
public string Name
{
get { return name; }
set { name = value; }
}
public int ID
{
get { return id; }
set { id = value; }
}
}
public class GenericList<T> where T : Employee
{
private class Node
{
private Node next;
private T data;
public Node(T t)
{
next = null;
data = t;
}
public Node Next
{
get { return next; }
set { next = value; }
}
public T Data
{
get { return data; }
set { data = value; }
}
}
private Node head;
public GenericList() //constructor
{
head = null;
}
public void AddHead(T t)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
public IEnumerator<T> GetEnumerator()
{
Node current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}
public T FindFirstOccurrence(string s)
{
Node current = head;
T t = null;
while (current != null)
{
//The constraint enables access to the Name property.
if (current.Data.Name == s)
{
t = current.Data;
break;
}
else
{
current = current.Next;
}
}
return t;
}
}
『玖』 如何过滤一个泛型集合中的相同数据
你的意思是不是record中有重复项??
如果是的话,你这一这样做。
List<T> list = .findAll();
Set<T> set = new HashSet<T>();
set.addAll(list);
这里的set中就没有回重复项了。
如果还答行得到list,你可以这样做。
List<T> list2 = new ArrayList<T> ();
list.addAll(set);
『拾』 C# 自定义泛型集合
我不知道你为什么要这样物汪核使用,不过你可以改成这样
Cells c=new Cells();
List<Cells> r=new List<Cells>();
r.add(c);
List<List<Cells>> list = new List<List<陵陆Cells>>();
list.add(r);
如果我说的符合你的要求,罩掘就给我分吧。- -