LinQ_MVC5_Ch3.5

LinQ_MVC5_Ch3.5

LinQ_MVC5_Ch3.5

 

  • Where()
  • string.Contains(string)   ->   like

            notice: 

string s1 ="test string contains";
string s2="";
bool test = s1.string.Contains(s2);   // true

     參考'網址: Will 、 MSDN

  • select()
  1. 回傳一個物件
  2. 裝入指定型別,重新組裝所需要的資料
    Private static IEnumerable<string> GetName()
    {
        var people = GerPeople();
        return prople.Select(person => person.Name);
    }
      
  • SelectMany()   ->  對原本集合與傳入集合以CROSS JOIN為主,每個元素與傳入的元素做一對一處理。
  • GroupBy()    ->  依照給的Key以及內容,產生分組資料
  • ToLookup()   ->  產生一個新的具有群組化特性集合物件,一個鍵值可對應多個實值。   <<沒有延遲執行特性>>
    var nameValuesGroup= new []
    {
        new { name = "Allen", value = 65, group = "A" },
        new { name = "Abbey", value = 120, group = "A" },
        new { name = "Slong", value = 330, group = "B" },
        new { name = "George", value = 213, group = "C" },
        new { name = "Meller", value = 329, group = "C" },
        new { name = "Mary", value = 192, group = "B" },
        new { name = "Sue" ,value = 200, group = "C"}
    };
    
    var lookupValues = nameValuesGroup.Tolookup(c => c.group);
    foreach (var g in lookupValues)
    {
       Console.WriteLine("===Group:{0}===",g.key);
       foreach (var item in q)
        {
         Console.WriteLine("name:{0},value:{1}",item.name ,item.value);
         }
    }
    
    //===Group:A===
    //name:Allen, value=65
    //name:Abbey, value=120
    //===Group:B===
    //name:Slong, value=330
    //name:Mary, value=192
    /===Group:C===
    //name:George, value=213
    //name:Meller, value=329
    //name:Sue, value=200
     範本參考: 開發美學MVC5  (3-29)
  • ToDictionary()
 public static List<Person> GetPersonList()
        {
            return new List<Person>()
            {
                new Person(){ID=1,Name="Kirk", Address="aaaa", Birthday=new DateTime(1985,6,8), Introduction="aaaa", Phone="0927896456",Group="A"},
                new Person(){ID=2,Name="John", Address="bbbb", Birthday=new DateTime(1972,1,3), Introduction="aaaa", Phone="0927111111",Group="A"},
                new Person(){ID=3,Name="David", Address="cccc", Birthday=new DateTime(1964,7,13), Introduction="aaaa", Phone="0927222222",Group="B"},
                new Person(){ID=4,Name="Steven", Address="dddd", Birthday=new DateTime(1982,6,1), Introduction="aaaa", Phone="0927333333",Group="B"},
                new Person(){ID=5,Name="Jason", Address="eeee", Birthday=new DateTime(1983,10,3), Introduction="aaaa", Phone="0927444444",Group="A"},
                new Person(){ID=6,Name="Titan", Address="ffff", Birthday=new DateTime(1987,7,2), Introduction="aaaa", Phone="0927555555",Group="C"},
                new Person(){ID=7,Name="Sophia", Address="gggg", Birthday=new DateTime(1981,3,27), Introduction="aaaa", Phone="0927666666",Group="A"},
                new Person(){ID=8,Name="Mary", Address="hhhh", Birthday=new DateTime(1982,12,18), Introduction="aaaa", Phone="0927777777",Group="D"},
                new Person(){ID=9,Name="Tom", Address="iiii", Birthday=new DateTime(1983,9,23), Introduction="aaaa", Phone="0927888888",Group="B"},
                new Person(){ID=10,Name="Julia", Address="jjjj", Birthday=new DateTime(1975,3,18), Introduction="aaaa", Phone="0927999999",Group="A"},
            };
        }

//===========================================================//

var personList = GetPersonList();
var PersonListDictionary = personList.ToDictionary(i=>i.ID);

foreach (var person in personListDictionary )
{
   Console.WriteLine("ID:{0}, Name:{1}",person.key, person.Value.Name)
}

//ID: 1, Name: Kirk
//ID: 2, Name: John
//ID: 3, Name: David
//ID: 4, Name: Steven
//ID: 5, Name: Jason
//ID: 6, Name: Titan
//ID: 7, Name: Sophia
//ID: 8, Name: Mary
//ID: 9, Name: Tom
//ID: 10, Name: Julia

參考網址:小風

  • GroupJoin  -> join()  + GroupBy() 
class Person
{
    public string Name { get; set; }
}

class Phone
{
    public string PhoneNumber { get; set; }
    public Person Person { get; set; }
}

//=============================================

Person Peter = new Person() { Name = "Peter" };
Person Sunny = new Person() { Name = "Sunny" };
Person Tim = new Person() { Name = "Tim" };
Person May = new Person() { Name = "May" };

Phone num1 = new Phone() { PhoneNumber = "01-5555555", Person = Peter };
Phone num2 = new Phone() { PhoneNumber = "02-5555555", Person = Sunny };
Phone num3 = new Phone() { PhoneNumber = "03-5555555", Person = Tim };
Phone num4 = new Phone() { PhoneNumber = "04-5555555", Person = May };
Phone num5 = new Phone() { PhoneNumber = "05-5555555", Person = Peter };

//=============================================

//Join()

var results = persons.Join(
    phones,
    person => person,
    phone => phone.Person,
    (person, phone) => new { person.Name, phone.PhoneNumber })
    .GroupBy(x => x.Name,
        (name, data) => new {
            Name = name,
            PhoneNumber = string.Join(',', data.Select(x => x.PhoneNumber)) });

//GroupJoin()
var results = persons.GroupJoin(
    phones,
    person => person,
    phone => phone.Person,
    (person, phoneEnum) =>
        new {
            person.Name,
            PhoneNumber = string.Join(',', phoneEnum.Select(x => x.PhoneNumber))
        }
);

//=================================================
/*
 * output:
 *
 * Peter: 01-5555555,05-5555555
 * Sunny: 02-5555555
 * Tim: 03-5555555
 * May: 04-5555555
 */

參考網址:iT

  • OrderBy() 
  • ThenBy()
  • Skip()  -> 將核心直接指定位置
  • SkipWhile()  -> 多判斷式,跳過符合條件的元素
  • Take()    ->  傳回集合中特定數量的元素
  • TakeWhile()   -> 傳回滿足條件的元素
  • Intersect()   -> LinQ交集
  • Except()      -> LinQ交集
  • Contain()   ->  判斷集合內有沒有特定值
  • Count()
  • Any()   -> 判斷集合內是否有存在一筆,不須完整計算