2016年7月31日日曜日

Google検索でグラフが表示できる

Google検索で関数を入力すると、グラフを表示してくれる。 log(x),y=x,y=x^2と入力した場合の結果です。

2016年7月28日木曜日

c#文字列比較

c#では、Equalsではなく、==を使って文字列比較したほうが良さそうだね。
タイプセーフ、nullで比較した時に落ちない。Stringクラスの中で、==で比較できるように、Equalsを再定義してるようだ。
http://sinproject.blog47.fc2.com/blog-entry-38.html


C# == str1 == str2
VB.NE = str1 = str2
Java .equals str1.equals(str2)
VB6/VBA = str1 = str2
↑Option Compare {Binary|Text|Database}のモードによる
Binary  

バイナリ文字コード順によって並べ替え順序が決まります。
Option Compare ステートメントの記述がない場合はこの Binary モードが使用されます
Text  

50音順による並べ替え順になります。
ただし、大文字と小文字、文字幅、カタカナとひらがなは区別しません。

2016年7月21日木曜日

jQueryでKey Valueのテーブルから、KeyでValueを検索

1.Tableの要素を取得
2.find("td.class")でKeyが入ってるtdを取得
3.eachでごりごり回しながら、Keyとマッチ
4.マッチしたら、next()で隣の要素を取得
5. return false;でeachから抜ける

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>HTML5サンプル</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="   crossorigin="anonymous"></script>
<script>
$(document).ready(function(){
//alert("hello");

 //検索ボタンクリック
 $(document).on("click", "[id='seach']", function() {
  $('#testTable').each(function () {
   $(this).find('td.code').each(function () {
    $(this).next().attr('style', '');
   });
   $(this).find('td.code').each(function () {
    if($(this).text() == $('#search_word').val()) {
    //alert($(this).next().text());
      $(this).next().attr('style', 'background-color:yellow');
      return false;
    } else {
     $(this).next().attr('style', '');
    }
   });
  });
 });


/*
 // grep
 $(document).on("click", "[id='seach']", function() {
  $('#testTable').each(function () {
   $(this).find('td').each(function () {
    if($(this).text().match(new RegExp($('#search_word').val(),"g"))) {
     $(this).attr('style', 'background-color:yellow');
    } else {
     $(this).attr('style', '');
    }
   });
  });
 });
*/
});
</script>
</head>
<body>
<br />
テキストボックスに検索したい文字を入力&#12289;検索ボタンをクリックすると&#12289;
<br />
入力した文字列が含まれるセルの背景色が黄色に変わります&#12290;<br />
<br />
<input type="text" id="search_word"">
<button type="button" id="seach"> Search </button>
<table id="testTable" border ="1">
<tr>
<th>Key</th><th>Value</th>
</tr>
<script type="text/javascript">
for (i=0; i<=10;i++){
    document.write("<tr>");
    document.write("<td class=\"code\">" + i + "</td>");
    document.write("<td>" + Math.floor(Math.random() *1000) + "</td>");
    document.write("</tr>");
}
</script>
</table>
</body>
</html>

LINQ Where句動的 AsQueryable()を使う。と可読性が増す

int? categoryID = null;
string categoryName = string.Empty;
string description = string.Empty;

// nullチェックとORでくっつけてやる
from c in ctx.Categories
where (categoryID == null || c.CategoryID == categoryID)
&& (string.IsNullOrEmpty(categoryName) || c.CategoryName == categoryName)
&& (string.IsNullOrEmpty(description) || c.Description == description)
select c;
↓でWhere句を動的に書ける。
var q = ctx.Categories.AsQueryable();
if (categoryID != null)
{
    q = q.Where(c => c.CategoryID == categoryID);
}
if (!string.IsNullOrEmpty(categoryName))
{
    q = q.Where(c => c.CategoryName == categoryName);
}

2016年7月20日水曜日

LINQ 条件文でフィルタをかける

// To run this sample, first specify an integer value of 1 to 4 for the command
// line. This number will be converted to a GradeLevel value that specifies which
// set of students to query. 
// Call the method: QueryByYear(args[0]);

static void QueryByYear(string level)
{
    GradeLevel year = (GradeLevel)Convert.ToInt32(level);
    IEnumerable<Student> studentQuery = null;
    switch (year)
    {
        case GradeLevel.FirstYear:
            studentQuery = from student in students
                           where student.Year == GradeLevel.FirstYear
                           select student;
            break;
        case GradeLevel.SecondYear:
            studentQuery = from student in students
                           where student.Year == GradeLevel.SecondYear
                           select student;
            break;
        case GradeLevel.ThirdYear:
            studentQuery = from student in students
                           where student.Year == GradeLevel.ThirdYear
                           select student;
            break;
        case GradeLevel.FourthYear:
            studentQuery = from student in students
                           where student.Year == GradeLevel.FourthYear
                           select student;
            break;

        default:
            break;
    }
    Console.WriteLine("The following students are at level {0}", year.ToString());
    foreach (Student name in studentQuery)
    {
        Console.WriteLine("{0}: {1}", name.LastName, name.ID);
    }
}

LINQ Contains

class DynamicPredicates : StudentClass
{
    static void Main(string[] args)
    {
        string[] ids = { "111", "114", "112" };

        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }

    static void QueryByID(string[] ids)
    {
        var queryNames =
            from student in students
            let i = student.ID.ToString()
            where ids.Contains(i)
            select new { student.LastName, student.ID };

        foreach (var name in queryNames)
        {
            Console.WriteLine("{0}: {1}", name.LastName, name.ID);
        }
    }
}

2016年7月10日日曜日

LINQ TO SQL でのINSERT

//自動採番の場合は、以下のプロパティ設定
//Auto Generated Value:True
//Auto-Sync:OnInsert
 var devDb2 = new DevDataClassesDataContext(devCon);
 User UserObj = new User();
      

// UserObj.UserID = 1;//自動採番するように設定されてるのでいらない
  UserObj.FirstName = "Hiroshi";
  UserObj.LastName = "Igarashi";
  UserObj.NickName = "IgaChan";

   devDb2.Users.InsertOnSubmit(UserObj);
   devDb2.SubmitChanges();