Thursday, October 8, 2015
Consider this very simple query (Assuming a scenario that not all the TimesheetLines are associated with a Job)
1
2
3
| Select TL.EntryDate, TL.Hours, J.JobName From TimeSheetLines TL Left Join Jobs J on TL.JobNo=J.JobNo |
A LINQ query using inner join is
1
2
3
4
5
6
7
8
9
10
11
| var lines = from tl in db.TimeSheetLines join j in db.Jobs on tl.JobNo equals j.JobNo where tl.ResourceNo == resourceNo select new { EntryDate = tl.EntryDate, Hours = tl.Hours, Job = j.JobName }; |
And a LINQ query performing left join is
1
2
3
4
5
6
7
8
9
10
11
12
| var lines = from tl in db.TimeSheetLines join j in db.Jobs on tl.JobNo equals j.JobNo into tl_j where tl.ResourceNo == resourceNo from j in tl_j.DefaultIfEmpty() select new { EntryDate = tl.EntryDate, Hours = tl.Hours, Job = j.JobName }; |
Notice that the only difference is the use of “
into
” with the join statement followed by reselecting the result using “DefaultIfEmpty
()” expression. And here’s the generated SQL for the above LINQ expression.
1
2
3
4
| SELECT [t0].[EntryDate] as [EntryDate], [t0].[Hours] as [Hours], [t1].[JobName] AS [Job] FROM [dbo].[TimeSheetLine] AS [t0] LEFT OUTER JOIN [dbo].[Jobs] AS [t1] ON [t0].[JobNo] = [t1].[JobNo] WHERE [t0].[ResourceNo] = @p0 |
Another LINQ version which is more compact is:
1
2
3
4
5
6
7
8
9
| var lines = from tl in db.TimeSheetLines from j in db.Jobs.Where(j=>j.JobNo == tl.JobNo).DefaultIfEmpty() select new { EntryDate = tl.EntryDate, Hours = tl.Hours, Job = j.JobName }; |
Similarly, this concept can be expanded for multiple left joins. Assuming that a TimeSheetLine will either have a JobNo or an IndirectCode, consider this SQL query:
1
2
3
4
| Select TL.EntryDate, TL.Hours, J.JobName, I.IndirectName From TimeSheetLines TL Left Join Jobs J on TL.JobNo=J.JobNo Left Join Indirects I on TL.IndirectCode=I.IndirectCode |
The equivalent LINQ query is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| var lines = from tl in db.TimeSheetLines join j in db.Jobs on tl.JobNo equals j.JobNo into tl_j join i in db.Indirects on tl.IndirectCode equals i.IndirectCode into tl_i where tl.ResourceNo == resourceNo from j in tl_j.DefaultIfEmpty() from i in tl_i.DefaultIfEmpty() select new { EntryDate = tl.EntryDate, Hours = tl.Hours, Job = j.JobName, Indirect = i.IndirectName, }; |
And the generated SQL is:
1
2
3
4
| SELECT [t0].[EntryDate] as [EntryDate], [t0].[Hours] as [Hours], [t1].[JobName] AS [Job], [t2].[IndirectName] As [Indirect] LEFT OUTER JOIN [dbo].[Jobs] AS [t1] ON [t0].[JobNo] = [t1].[JobNo] LEFT OUTER JOIN [dbo].[Indirects] AS [t2] ON [t0].[IndirectCode] = [t2].[IndirectCode] WHERE [t0].[ResourceNo] = @p0 |
That’s all, left outer joins in LINQ are as easy as in T-SQL. Happy joining.
Update:
Notice that this post describes the approach to perform a Left Outer Join in LINQ To SQL as well as Entity Framework (version 4). The same is not true for Entity Framework version 3.5 since it does not support the
Notice that this post describes the approach to perform a Left Outer Join in LINQ To SQL as well as Entity Framework (version 4). The same is not true for Entity Framework version 3.5 since it does not support the
DefaultIfEmpty
keyword. To perform Left Outer Joins with Entity Framework 3.5, we need to create appropriate relationships (e.g 0..1 to 0..Many) in our Entity Model and they will be automatically translated into TSQL’s Left Join clause.Saturday, October 3, 2015
Class for paging for generic collections
Import namespaces
using System.Collections; using System.Collections.Generic; using System.Linq;
Class for paging
public class PagingCollectionpublic int PageSize { get { return _pageSize; } set { if (value <= 0) { throw new ArgumentException(); } _pageSize = value; } } ///: IEnumerable { #region fields private const int DefaultPageSize = 10; private readonly IEnumerable _collection; private int _pageSize = DefaultPageSize; #endregion #region properties /// /// Gets or sets page size ///
Use
// fill test data var data = new List<int>(); for (int i = 0; i < 25; i++) { data.Add(i); } // create paging collection var paging = new PagingCollection<int>(data); // set page size paging.PageSize = 6; // get number of pages Console.WriteLine("Pages count: {0}", paging.PagesCount); // iterate through pages for (int i = 1; i <= paging.PagesCount; i++) { // get number of items on page Console.WriteLine("Page: {0} ({1} items)", i, paging.GetCount(i)); // get data by page number foreach (int number in paging.GetData(i)) { Console.WriteLine(number); } }
Making the Header Fixed and Rows Scrollable in Repeater
The Repeater’s Header will be fixed and rows will be made Scrollable using jQuery Scrollable Table plugin.
The plugin will be applied to the Table element.
Tuesday, September 29, 2015
Để lấy được nội dung website thông qua một URL bất kì. Việc đầu tiên ta phải lấy về được toàn bộ html của link đó, sau đó phân tích chuỗi html đó, dựa vào các id hoặc name của div, table, span, v.v.... mà ta sẽ lấy được nội dung cần lấy. Để làm được việc này các bạn cần có một chút kiến thức về RegularExpressions.
Do mỗi website có cấu trúc khác nhau nên cách trích lọc và phân tích html cũng khác nhau. Ở đây tôi sẽ ví dụ cho các bạn lấy nội dung tin tức từ website vnexpress.net. Cụ thể tôi sẽ lấy nội dung của link sau: http://vnexpress.net/gl/xa-hoi/2012/08/phu-nu-chet-loa-the-trong-khach-san/
Đầu tiên, các bạn add thêm 4 thư viện sau:
using System.Text.RegularExpressions;
using System.Text;
using System.Net;
using System.IO;
Hàm lấy html của link bất kì:
public string GetWebContentHTML(string strLink)
{
string strContent = "";
try
{
WebRequest objWebRequest = WebRequest.Create(strLink);
objWebRequest.Credentials = CredentialCache.DefaultCredentials;
WebResponse objWebResponse = objWebRequest.GetResponse();
Stream receiveStream = objWebResponse.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, System.Text.Encoding.UTF8);
strContent = readStream.ReadToEnd();
objWebResponse.Close();
readStream.Close();
}
catch (Exception ex)
{
return ex.Message;
}
return strContent;
}
Bây giờ công việc tiếp theo là lấy ra từng thành phần mà bạn muốn lấy.
Phân tích lấy phần tiêu đề: Các bạn viết hàm sau:
public string LayTieuDe(string Content)
{
string pattern = "
[^<]+"
;
Regex Title = new Regex(pattern);
Match m = Title.Match(Content);
if (m.Success)
return m.Value.Substring(16, m.Value.Length - 16);
return "";
}
Phân tích lời phần mô tả: Hàm được viết tương tự như sau:
public string LayMoTa(string Content)
{
string pattern = "
[^<]+"
;
Regex Title = new Regex(pattern);
Match m = Title.Match(Content);
if (m.Success)
return m.Value.Substring(15, m.Value.Length - 15);
return "";
}
Phân tích lấy phần nội dung. Các bạn tạo hàm sau:
public string LayNoiDung(string Content)
{
string pattern = "
[^~]+"
;
Regex Title = new Regex(pattern);
Match m = Title.Match(Content);
if (m.Success)
return m.Value.Substring(16, m.Value.Length - 16).Replace("/Files", "http://vnexpress.net/Files").Replace("/gl","http://vnexpress.net/gl");
return "";
}
Bạn sử dụng một iframe trong website để hiển thị nội dung (nội dung một website khác hoặc nội dung từ các file tài liệu, ....) nhưng bạn gặp một vấn đề về chiều cao của iframe.
Nếu bạn đặt thuộc tính height = "auto" thì iframe không tự động co giãn chiều cao theo nội dung nó chứa mà chỉ hiển thị một phần rất nhỏ. Nếu bạn đặt height = "100%" thì cũng không tác dụng. Còn nếu bạn đặt height="n px" (n là một giá trị cố định bất kì) thì đôi lúc nội dung quá ngắn sẽ xuất hiện một khoảng trắng bên dưới rất xấu, hoặc khi nội dung quá dài thì iframe không hiển thị được hết nội dung.
Toàn Cầu Web sẽ hướng dẫn các bạn dùng Jquery để iframe tự động fix chiều cao theo nội dung bên trong nó.
Đầu tiên bạn cần tải jquery tại: jquery.com khai báo Jquery:
<script language="javascript" type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
Sau đó bạn dán javascript sau và trang web của bạn:
<script type="text/javascript">
//function to fix height of iframe!
var calcHeight = function () {
var headerDimensions = $(#mainlivedemo).height();
var selector = ($(.stretched).length > 0) ? #iframelive : #iframelive iframe;
$(selector).height($(window).height() - headerDimensions);
}
$(document).ready(function () {
calcHeight();
});
$(window).resize(function () {
calcHeight();
}).load(function () {
calcHeight();
});
</script>
Bạn lưu ý ID "iframelive", iframe của bạn sẽ được đặt bên trong một DIV có ID "iframelive":
<div id="iframelive">
<iframe src="http://toancauweb.com/" style="width:100%; border:none;"></iframe>
</div>
Như vậy là bạn đã tạo được một iframe có chiều cao tự động fix với nội dung.
Bạn nhận một đơn đặt hàng thiết kế website deal yêu cầu có một đồng hồ thời gian đếm ngược (VD: đấu giá, thời gian khuyến mãi, v.v...) nhưng làm thế nào chèn một bộ đếm thời gian vào website và mỗi khi thời gian nhích một giây vẫn không làm cho trang web bị load lại. Toàn Cầu Web xin trình bày một cách đơn giản để có thể thực hiện được điều này.
Tại trang Default.aspx, bạn chèn code sau vào phần source HTML:
<div id="timelabel"></div>
<script type="text/javascript">
var leave =<%=seconds %>;
CounterTimer();
var interv=setInterval(CounterTimer,1000);
function CounterTimer()
{
var day = Math.floor(leave / ( 60 * 60 * 24))
var hour = Math.floor(leave / 3600) - (day * 24)
var minute = Math.floor(leave / 60) - (day * 24 *60) - (hour * 60)
var second = Math.floor(leave) - (day * 24 *60*60) - (hour * 60 * 60) - (minute*60)
hour=hour<10 nbsp="" span="" style="color: maroon; margin: 0px;">"0"10> + hour : hour;
minute=minute<10 nbsp="" span="" style="color: maroon; margin: 0px;">"0"10> + minute : minute;
second=second<10 nbsp="" span="" style="color: maroon; margin: 0px;">"0"10> + second : second;
var remain=day + " days "+hour + ":"+minute+":"+second;
leave=leave-1;
document.getElementById("timelabel").innerHTML=remain;
}
</script>
Sau đó bạn chèn code sau vào trang Default.aspx.cs:
public double seconds;
protected void Page_Load(object sender, EventArgs e)
{
seconds = (GetEndTime() - GetStartTime()).TotalSeconds;
}
private DateTime GetStartTime()
{
return DateTime.Now;
}
private DateTime GetEndTime()
{
return new DateTime(2010, 5, 06, 8, 10, 0);
}
Sunday, August 9, 2015
Hình ảnh bạn bắt gặp.
Bạn có thể giải quyết trường hợp này như sau.
Mở IIS Manager và kiểm tra Application Pool trong đó.
Như bạn đã thấy tại cột Status, có 3 application pool liên quan đến SharePoint đang ở trạng thái ngưng hoạt động (Stopped). Để bật lại bạn chỉ cần r-click vào Application pool và chọn Start.
Ngoài ra, vẫn còn nguyên nhân khác dẫn đến lỗi. R-click vào Application pool và chọn Advanced Settings..Chọn giá trị False tại Enable 32-Bit Application.
Nếu như bạn đã đổi mật khẩu của tài khoản làm Application pool, bạn cần thiết lập lại.
Subscribe to:
Posts (Atom)