private struct fun_MyPre
        {
            //存储权限的结构
            public Int32 iPre;
            public int Tag;
        }
已知Tag的值,如何知道对应iPre的值??
PS:Tag的值不会重复,但也没有排序关系

解决方案 »

  1.   

    不用使用结构,使用Dictionary
    Dictionary<int,int>  map = new Dictionary<int,int>();
    map.Add(tag1,ipre1);
    map.Add(tag2,ipre2);
    .....int toFindByTag = ...;
    int iPreResult = map[toFindByTag];
      

  2.   

    你是用这个 数据体 来存取 数据,然后再数据体内进行 查询吗?
    这样的问题 只能 用 循环 来比较了吧  
    用ArrayList 存储你的数据 然后用循环 访问 
    希望能帮助你
      

  3.   

    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;
    }
     
      

  4.   

    说道查询,就应该用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;
            }
      

  5.   

    TO4楼的错误 1 可访问性不一致: 参数类型“System.Collections.Generic.List<_3ELink.fomUser.fun_MyPre>”比方法“_3ELink.fomUser.fun_GetPre(System.Collections.Generic.List<_3ELink.fomUser.fun_MyPre>, int)”的可访问性低 F:\Client\3ELink\fomUser.cs 122 20 3ELink
      

  6.   

    我把private struct fun_MyPre声明为public之后就错误 1 “_3ELink.fomUser.fun_MyPre”不包含“Where”的定义,并且找不到可接受类型为“_3ELink.fomUser.fun_MyPre”的第一个参数的扩展方法“Where”(是否缺少 using 指令或程序集引用?) F:\Client\3ELink\fomUser.cs 125 20 3ELink
      

  7.   

    TO#6楼
    你把private struct fun_MyPre改为public的吧,本来定义一个结构体就要多出访问,定义为私有的做什么呢?我那个地方直接复制了你的结构体定义而已,所以出现访问性不一致。
      

  8.   

    谢谢,最后有个问题,这个函数怎么引用呢?
    List<fun_MyPre> pre;
    int ii = fun_GetPre(pre,  Convert.ToInt32(lv_User.Tag));
    我这样写的话就
    错误 1 使用了未赋值的局部变量“pre” F:\Client\3ELink\fomUser.cs 296 33 3ELink
      

  9.   

    好了,初始化好了
    现在一开始结构体的赋值又出现问题了,大侠再帮帮忙,我想是最后一个问题了吧
    List<fun_MyPre> p;
    p.iPre = Convert.ToInt32(myDr["c_premissions"]);//权限
    p.Tag = Convert.ToInt32(lv.Tag);
    这样赋值不允许啊
      

  10.   

    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);
      

  11.   

    p.Tag = Convert.ToInt(lv.Tag);你的Tag对象是int,不是int32
      

  12.   

    或者p.Tag = int.Parse(lv.Tag);
      

  13.   

    以上,按照13楼的写法
    编译没问题了
    但是查询的时候
    public int fun_GetPre(List<fun_MyPre> mypre, int tag)
    {
                //查询权限结构体
        return mypre.Where((t) => t.Tag == tag).First().iPre;//卡在这里说序列不包含任何元素
    }
      

  14.   

    好了好了,谢谢大家
    把Lst作为全局就可以了
    谢谢大家
      

  15.   


            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();
            }
      

  16.   

    用Linq就很简单了比如你的这个结构体存放在 List<> 或是 数组 funMyPrelist中那你就可以这样var items = (from para in funMyPrelist where para.Tag == "指定的Tag值" select para)fun_MyPre fm = items.First() as fun_MyPre;
    这个fm就是你要找的那个结构体了,不过他有可能为空
      

  17.   


    回去加强下基础支持,int就是Int32,在.NET CLR里,这两种写法的等价的。