开发者

where on LINQ query

开发者 https://www.devze.com 2023-03-17 02:35 出处:网络
I have the next code: b.Text = myDataContext.purchases.Count().ToString(); the \"b\" is label that i have on aspx page.

I have the next code:

b.Text = myDataContext.purchases.Count().ToString();

the "b" is label that i have on aspx page.

I want to add to the code : where items.main == true , like i have here:

        var bla = from items in myDataContext.items
                   where items.main == true
                   select items;

How can i do that on the : b.Text = myDataContext.purchases.Count().ToString();

i have table : 开发者_如何学Citems with column itemId and column main (bit).

and table: purchase.

on purchase i have column itemId (with relationship)


There is an overload of Count() that takes a predicate (filter); and the == true is redundant, so if the main is part of the purchase:

b.Text = myDataContext.purchases.Count(p => p.main).ToString();

With the edit, you will need to join, either through a helper member:

b.Text = myDataContext.purchases.Count(p => p.item.main).ToString();

Or manually:

  b.Text = (from p in myDataContext.purchases
            join i in myDataContext.items on p.itemId equals i.itemId
            where i.main
            select p).Count().ToString();
0

精彩评论

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