顯示具有 Linq 標籤的文章。 顯示所有文章
顯示具有 Linq 標籤的文章。 顯示所有文章

2015年6月16日 星期二

Linq 相同Group 中資料分隔

Data:
Name No Qty
A        1     50
A        2     60
B       12    10
C       13    70

Result:
A  1,2  110
B   12    10
C   13    70



var groupResult = data.ToLookup(s => s.Name);

string symbol=",";

foreach (var c in groupCPSPNResult)

{

    result.Add(new

   {

      Name= c.Key.Name,

      No= string.Join(symbol, c.Select(s => s.No).ToArray()),

      Total= c.Sum(s => s.Qty)

  });

}

2011年6月9日 星期四

linq 教學(2) group by

--
Imports System.Linq
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mm() As Integer = {2, 2, 3, 1, 3, 8, 63, 2}
Dim qm = From xm In mm Select xm Group By xm Into Group
TextBox1.Text = ""
For Each a In qm
TextBox1.Text += a.xm.ToString + " || " + a.Group.ToString + vbCrLf
Next

End Sub
End Class

linq 教學(1) where

--
一般使用linq,若不再搭配其它資料或運用的話;可以用單純的 LINQPad 工具來寫linq
--
Sub Main
dim i() as integer={1,2,3,4,5,6}
dim x =from a in i where a>2 select a
for each b in x
console.writeline(b)
next
End Sub

2011年3月10日 星期四

LINQPad 工具

LINQPad
---
用過的,都說:讚!!

linq 自訂 sqlconnect string

參考
--
try

{

string connectionString = @"Data Source=server_name;database=database_name;Integrated Security=True;";

using (SqlConnection connection = new SqlConnection(connectionString))

{

connection.Open();

Mydb db = new Mydb(connection);

var q = from c in db.Customers

where c.CustomerID.StartsWith("A")

select new { c.CustomerID, c.Phone };



foreach(var c in q)

Debug.WriteLine(c);

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}