编辑器加载中...

1. oneTT有字段AA BB CC

2. twoTT有字段EE FF BB

两个表连接查询,

List<MulTable> mul = (from a in _db.oneTTT
                      join b in _db.twoTTT
                      on a.BB equals b.BB
                      where a.BB != ""
                      select new MulTable
                      {
                          AAA = a.AA,
                          BBB = a.BB,
                          EEE = b.EE,
                          FFF = b.FF
                      }).ToList<MulTable>();



            foreach (MulTable item in mul)
                Console.WriteLine(item.AAA + item.BBB + item.EEE);
class MulTable
    {
        public string AAA = string.Empty;
        public string BBB = string.Empty;
        public string EEE = string.Empty;
        public string FFF = string.Empty;

        public override string ToString()
        {
            return string.Format("AAA: {0}, BBB:{1}, EEE:{2},FFF{3}", AAA, BBB, EEE,FFF);
        }
    }
string[] Weekdays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
            //see the above code, the IEnumerable<string> WeekdaysWithLength is the explicit object,here var WeekdaysWithLength is implicit object, which will convert by assign type.
           // var WeekdaysWithLength = from W in Weekdays where W.Length > 1 orderby W select W;
            string[] WeekdaysWithLength = (from W in Weekdays where W.Length > 1 orderby W select W).ToArray<string>();
            //listBox1.Items.Clear();
            foreach (string str in WeekdaysWithLength)
                Console.WriteLine(str);

            List<Car> myCars = new List<Car> { 
            new Car{ Color= "Silver", Make="BMW", PetName="Henry", Speed=100},
            new Car{ Color= "Tan", Make="BMW", PetName="Daisy", Speed=90},
            new Car{ Color= "Black", Make="VW", PetName="Mary", Speed=55},
            new Car{ Color= "Rust", Make="Yugo", PetName="Clunker", Speed=5},
            new Car{ Color= "White", Make="Ford", PetName="Melvin", Speed=43}
            };

            List<Car> fastCars = (from fCar in myCars where fCar.Speed > 55 select fCar).ToList<Car>();
            foreach (Car c in fastCars)
                Console.WriteLine(c.PetName);

            //nongeneric
            // non generic collection of Car objects
            ArrayList arCars = new ArrayList{ 
            new Car{ Color= "Silver", Make="BMW", PetName="Henry", Speed=100},
            new Car{ Color= "Tan", Make="BMW", PetName="Daisy", Speed=90},
            new Car{ Color= "Black", Make="VW", PetName="Mary", Speed=55},
            new Car{ Color= "Rust", Make="Yugo", PetName="Clunker", Speed=5},
            new Car{ Color= "White", Make="Ford", PetName="Melvin", Speed=43}
            };

            //convert non generic car object collection to enumerable
            //IEnumerable<Car> myCars = arCars.OfType<Car>();
            //List<Car> fastCars = (from fCar in myCars where fCar.Speed > 55 select fCar).ToList<Car>();
            //foreach (Car c in fastCars)
            //    Console.WriteLine(c.PetName);

posted on 2011-04-22 16:18  Black Bean  阅读(8069)  评论(0编辑  收藏  举报