class student
        {
            public string no { get; set; }
            public string name { get; set; }
            public string tel { get; set; }
            public string address { get; set; }
            public override string ToString()
            {
                return string.Format("{0} {1} {2} {3}", no, name, tel, address);
            }
        }
 List<student> mylist = new List<student>();
 string current = textBox2.Text;
            int ci=  mylist.IndexOf((student)from a in mylist where a.name==current select a);
发生以下异常:
附:完整代码:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Collections;namespace myconnect
{
   
    public partial class connect : Form
    {
        public connect()
        {
            InitializeComponent();
        }
        class student
        {
            public string no { get; set; }
            public string name { get; set; }
            public string tel { get; set; }
            public string address { get; set; }
            public override string ToString()
            {
                return string.Format("{0} {1} {2} {3}", no, name, tel, address);
            }
        }
        List<student> mylist = new List<student>();
        private void connect_Load(object sender, EventArgs e)
        {
            string filename = "myconnect.txt";
            FileStream f1 = new FileStream(filename,FileMode.OpenOrCreate);
            StreamReader sr = new StreamReader(f1,Encoding.GetEncoding("gb2312"));
            string hasread;
         
            string[] spilt;
            while ((hasread = sr.ReadLine()) != null)
            {
                int i = 0;
             spilt = hasread.Split(',');
             mylist.Add(new student {no=spilt[0],name=spilt[1],tel=spilt[2],address=spilt[3] });
             i++;
               
            }
            if (mylist.Count==0)
            {
                return;
            }
            textBox1.Text = mylist[0].no;
            textBox2.Text = mylist[0].name;
            textBox3.Text = mylist[0].tel;
            textBox4.Text = mylist[0].address;
         
            
        }        private void btnLast_Click(object sender, EventArgs e)
        {
            string current = textBox2.Text;
            int ci=  mylist.IndexOf((student)from a in mylist where a.name==current select a);
            //if (ci - 1 > 0)
            //{
            //    textBox1.Text = mylist[ci - 1].no;
            //    textBox2.Text = mylist[ci - 1].name;
            //    textBox3.Text = mylist[ci - 1].tel;
            //    textBox4.Text = mylist[ci - 1].address;
            //}
            //else
            //{
            //    return;
            //}
        }
    }
}
list

解决方案 »

  1.   

     private void btnLast_Click(object sender, EventArgs e)      
      {          
      string current = textBox2.Text;   
     int ci=  -1;        
      var res=mylist.Select((x,i)=>new {name=x.name,i}).Where(x=>x.name==current).First();
    if(res!=null)
       ci=res.i;
    }
      

  2.   

    int ci = mylist.IndexOf((from a in mylist where a.name==current select a).First());
      

  3.   

    当然,你这么写太罗嗦,这样就可以了:
    int ci = mylist.IndexOf(mylist.First(a => a.name == current));
      

  4.   

    var index=list.Select((x,index)=>new {name=x.name,index=index}).Where(x=>x.name==current).First().index;