fun_MyPre[] fs = new fun_MyPre[]{ //... };private fun_MyPre FindByTag(int tag){ for( int i = 0; i < fs.Count; i++) if(fs[i].Tag == tag) return fs[i]; return null; }
说道查询,就应该用Linq,那个专门用来查询的。 using System.Linq; private struct fun_MyPre { //存储权限的结构 public Int32 iPre; public int Tag; } public int getipre(List<fun_MyPre> mypre,int tag) { return mypre.Where((t) => t.Tag == tag).First().iPre; }
List <fun_MyPre> lst=new List <fun_MyPre>(); fun_MyPre p; p.iPre = Convert.ToInt32(myDr["c_premissions"]); p.Tag = Convert.ToInt32(lv.Tag); lst.Add(p);
p.Tag = Convert.ToInt(lv.Tag);你的Tag对象是int,不是int32
或者p.Tag = int.Parse(lv.Tag);
以上,按照13楼的写法 编译没问题了 但是查询的时候 public int fun_GetPre(List<fun_MyPre> mypre, int tag) { //查询权限结构体 return mypre.Where((t) => t.Tag == tag).First().iPre;//卡在这里说序列不包含任何元素 }
好了好了,谢谢大家 把Lst作为全局就可以了 谢谢大家
private struct fun_MyPre { //存储权限的结构 public Int32 iPre; public int Tag; public fun_MyPre(Int32 ipre, int tag) { this.iPre = ipre; this.Tag = tag; } } static void Main(string[] args) { List<fun_MyPre> pp = new List<fun_MyPre>(); pp.Add(new fun_MyPre(1, 10)); pp.Add(new fun_MyPre(2, 20)); pp.Add(new fun_MyPre(3, 30)); pp.Add(new fun_MyPre(4, 40)); pp.Add(new fun_MyPre(5, 50)); var q = pp.Where(e => e.Tag == 40).First(); Console.WriteLine("{0}", q.iPre); Console.ReadKey(); }
用Linq就很简单了比如你的这个结构体存放在 List<> 或是 数组 funMyPrelist中那你就可以这样var items = (from para in funMyPrelist where para.Tag == "指定的Tag值" select para)fun_MyPre fm = items.First() as fun_MyPre; 这个fm就是你要找的那个结构体了,不过他有可能为空
Dictionary<int,int> map = new Dictionary<int,int>();
map.Add(tag1,ipre1);
map.Add(tag2,ipre2);
.....int toFindByTag = ...;
int iPreResult = map[toFindByTag];
这样的问题 只能 用 循环 来比较了吧
用ArrayList 存储你的数据 然后用循环 访问
希望能帮助你
for( int i = 0; i < fs.Count; i++)
if(fs[i].Tag == tag) return fs[i];
return null;
}
using System.Linq; private struct fun_MyPre
{
//存储权限的结构
public Int32 iPre;
public int Tag;
} public int getipre(List<fun_MyPre> mypre,int tag)
{
return mypre.Where((t) => t.Tag == tag).First().iPre;
}
你把private struct fun_MyPre改为public的吧,本来定义一个结构体就要多出访问,定义为私有的做什么呢?我那个地方直接复制了你的结构体定义而已,所以出现访问性不一致。
List<fun_MyPre> pre;
int ii = fun_GetPre(pre, Convert.ToInt32(lv_User.Tag));
我这样写的话就
错误 1 使用了未赋值的局部变量“pre” F:\Client\3ELink\fomUser.cs 296 33 3ELink
现在一开始结构体的赋值又出现问题了,大侠再帮帮忙,我想是最后一个问题了吧
List<fun_MyPre> p;
p.iPre = Convert.ToInt32(myDr["c_premissions"]);//权限
p.Tag = Convert.ToInt32(lv.Tag);
这样赋值不允许啊
fun_MyPre p;
p.iPre = Convert.ToInt32(myDr["c_premissions"]);
p.Tag = Convert.ToInt32(lv.Tag);
lst.Add(p);
编译没问题了
但是查询的时候
public int fun_GetPre(List<fun_MyPre> mypre, int tag)
{
//查询权限结构体
return mypre.Where((t) => t.Tag == tag).First().iPre;//卡在这里说序列不包含任何元素
}
把Lst作为全局就可以了
谢谢大家
private struct fun_MyPre
{
//存储权限的结构
public Int32 iPre;
public int Tag;
public fun_MyPre(Int32 ipre, int tag)
{
this.iPre = ipre;
this.Tag = tag;
}
} static void Main(string[] args)
{
List<fun_MyPre> pp = new List<fun_MyPre>(); pp.Add(new fun_MyPre(1, 10));
pp.Add(new fun_MyPre(2, 20));
pp.Add(new fun_MyPre(3, 30));
pp.Add(new fun_MyPre(4, 40));
pp.Add(new fun_MyPre(5, 50)); var q = pp.Where(e => e.Tag == 40).First(); Console.WriteLine("{0}", q.iPre);
Console.ReadKey();
}
这个fm就是你要找的那个结构体了,不过他有可能为空
回去加强下基础支持,int就是Int32,在.NET CLR里,这两种写法的等价的。