具有布尔函数的Linq与Entity Framework中的关系数据库 [英] Linq with boolean function to relational db in Entity Framework

查看:243
本文介绍了具有布尔函数的Linq与Entity Framework中的关系数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码在这里可能有一些错误,但是我主要对语法有问题。条目是在条目中使用的模型,并且包含每个条目的时间戳。会员是分配了条目的人员的模型,并包含条目的fk。我想根据成员在给定时间段内任意选择的条目(任意选择30天)对我的成员名单进行排序。
A.我不知道我创建的功能是否正常工作,但是除了主要的一点,因为我还没有真正挖掘它。
B.我找不到Linq语句的语法,或者甚至可能。

Probably a few things wrong with my code here but I'm mostly having a problem with the syntax. Entry is a model for use in Entries and contains a TimeStamp for each entry. Member is a model for people who are assigned entries and contains an fk for Entry. I want to sort my list of members based off of how many entries the member has within a given period (arbitrarily chose 30 days). A. I'm not sure that the function I created works correctly, but this is aside from the main point because I haven't really dug into it yet. B. I cannot figure out the syntax of the Linq statement or if it's even possible.

功能:

        private bool TimeCompare(DateTime TimeStamp) 
    {
        DateTime bound = DateTime.Today.AddDays(-30);

        if (bound <= TimeStamp)
        {
            return true;
        }

        return false;
    }

会员列表:

    public PartialViewResult List()
    {
        var query = repository.Members.OrderByDescending(p => p.Entry.Count).Where(TimeCompare(p => p.Entry.Select(e => e.TimeStamp));

        //return PartialView(repository.Members);
        return PartialView(query);
    }

var查询是我的问题,我似乎找不到一种方法来合并一个布尔函数转换成一个linq中的.where语句。

the var query is my problem here and I can't seem to find a way to incorporate a boolean function into a .where statement in a linq.

编辑
总结一下只需尝试查询过去30天内所有时间戳的条目。

EDIT To summarize I am simply trying to query all entries timestamped within the past 30 days.

我还必须强调关系/ fk部分,因为这似乎是迫使时间戳记为IEnumerable System.Datetime而不是简单的System.Datetime。

I also have to emphasize the relational/fk part as that appears to be forcing the Timestamp to be IEnumerable of System.Datetime instead of simple System.Datetime.

这个错误与E.TimeStamp中的不能隐式地转换时间戳到bool:

This errors with "Cannot implicitly convert timestamp to bool" on the E.TimeStamp:

var query = repository.Members.Where(p => p.Entry.First(e => e.TimeStamp) <= past30).OrderByDescending(p => p.Entry.Count);

此操作符'='的错误不能应用于System.Collections类型的操作数。 Generic.IEnumerable'和'System.DateTime'

This errors with Operator '<=' cannot be applied to operands of type 'System.Collections.Generic.IEnumerable' and 'System.DateTime'

var query = repository.Members.Where(p => p.Entry.Select(e => e.TimeStamp) <= past30).OrderByDescending(p => p.Entry.Count);

EDIT2

EDIT2

语法上正确但不是语义学:

Syntactically correct but not semantically:

var query = repository.Members.Where(p => p.Entry.Select(e => e.TimeStamp).FirstOrDefault() <= timeComparison).OrderByDescending(p => p.Entry.Count);

所需的结果是拉取所有成员,然后按其所拥有的条目数排序,成员有条目,然后通过他们拥有的条目数量订单。基本上这个应该以某种方式嵌套在.count中。

The desired result is to pull all members and then sort by the number of entries they have, this pulls members with entries and then orders by the number of entries they have. Essentially the .where should somehow be nested inside of the .count.

EDIT3

EDIT3

语法正确但导致运行时错误(异常详细信息:System.ArgumentException:DbSortClause表达式必须具有可比较的类型
参数名称:key):

Syntactically correct but results in a runtime error (Exception Details: System.ArgumentException: DbSortClause expressions must have a type that is order comparable. Parameter name: key):

var query = repository.Members.OrderByDescending(p => p.Entry.Where(e => e.TimeStamp <= timeComparison));

EDIT4

EDIT4

更接近(因为这行编译),但它似乎没有对该对象有任何影响。无论我为用户添加了多少个条目,它不会根据需要更改排序顺序(或根本不会)。

Closer (as this line compiles) but it doesn't seem to be having any effect on the object. Regardless of how many entries I add for a user it doesn't change the sort order as desired (or at all).

        var timeComparison = DateTime.Today.AddDays(-30).Day;
        var query = repository.Members.OrderByDescending(p => p.Entry.Select(e => e.TimeStamp.Day <= timeComparison).FirstOrDefault());


推荐答案

感谢所有的帮助Dylan,但这里是最后答案:

Thanks for all the help Dylan but here is the final answer:

    public PartialViewResult List()
    {

        var timeComparison = DateTime.Today.AddDays(-30).Day;
        var query = repository.Members
                  .OrderBy(m => m.Entry.Where(e => e.TimeStamp.Day <= timeComparison).Count());

        return PartialView(query);
    }

这篇关于具有布尔函数的Linq与Entity Framework中的关系数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆