开发者

linq count optimization

I'm writing a linq query and I'm wondering if I'm doing it right: I'm passing in 2 parameters, TheDate and UserID

Var Output = from c in in MyDC.Table1
  where c.Datetime.Date == TheDate.Date
  where c.UserID == TheUserID
  select new MyMode()
  {
    Var1 = (from c1 in MyDC.Table1
            where c.ID == c1.ID
            select c1.ID).Count()
    Var2 = ...开发者_StackOverflow中文版

  }

I'm thinking I should be able to do the count for Var1 without going back to the original MyDC but for now that's what I have. Let me know if there's a better way to do it.

Thanks.


Wouldn't that be the same as:

Var1 = c.Count()

Are you needing to group on some value, too?


The count is an override using method-based syntax.

var defaults = new[] { "zero", "one", "two", "three", "four" };

var abc = defaults.Select((o,i) => i);
abc.Dump();

outputs: IEnumerable (5 items) 0 1 2 3 4

o is the object and i is the index. I usually use method syntax so I'm not sure on the conversion to it.


var result = from c in MyDC.Table1
             where c.Datetime.Date == TheDate.Date
             && c.UserID == TheUserID
             group c by c.ID into g
             select new
             {
                 Var1 = g.Count()
             };


If you group by c.DateTime.Date and c.UserId in the original query then the Var1 is just the count of items in the group.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜