开发者

Linq-to-sql - query is not filtered

开发者 https://www.devze.com 2023-01-09 13:02 出处:网络
I am really new to Linq and am using Linq-to-Sql as follows. However in the following example, my where clause never gets executed and the resultant query attempts to fetch all the records from my tab

I am really new to Linq and am using Linq-to-Sql as follows. However in the following example, my where clause never gets executed and the resultant query attempts to fetch all the records from my table, ignoring even the take method.

Can somebody point out as to what i am doing wrong

        var baseQry = db.Table;
        baseQry.Where(a => a.tab_id == theId);
        baseQry.Select(o => new
        {
            o.name,
            o.display_name,
            o.type,
            o.info,                
            time_stamp = (Convert.ToDateTime(o.timestamp).ToLongDateString())
        }).Take(10);

       baseQry.ToList();
开发者_JAVA技巧


Your second line...

baseQry.Where(a => a.tab_id == theId);

...is essentially a no-op, because the resulting query isn't carried over into your .Select clause.

You need to change it to this:

var baseQry = db.Table;

var results = baseQry
    .Where(a => a.tab_id == theId)
    .Select(o => new
        {
            o.name,
            o.display_name,
            o.type,
            o.info,                
            time_stamp = (Convert.ToDateTime(o.timestamp).ToLongDateString())
        })
    .Take(10)
    .ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消