English 中文(简体)
LINQ 无效行动 提问时的例外
原标题:LINQ InvalidOperationException when making a query

奥基,最后编辑,承诺:

课程如下:

public partial class MembershipModule : BaseConnection<MembershipModule>
{
    private const string AccessPrivilege = "Access";

    public bool Accessible()
    {
        return this.Privileges().Any(p => p.Name.Equals(AccessPrivilege));
    }

    public IQueryable<MembershipAction> Privileges()
    {
        var privileges = from p in LinqUtil.Context.MembershipModuleActions
                         where
                             p.MembershipModule.Id.Equals(this.Id) &&
                             p.MembershipRolePrivileges.Any(rp => rp.ModuleActionId.Equals(p.Id))
                         select p.MembershipAction;

        return privileges;
    }
}

为什么这项工作

    public static List<MembershipModule> Collection()
    {
        List<MembershipModule> collection = new List<MembershipModule>();

        if(!MembershipUser.Connected)
            return collection;

        foreach(MembershipModule m in LinqUtil.Context.MembershipModules)
        {
            if(m.Accessible())
                collection.Add(m);
        }

        return collection;
    }

虽然如此?

    public static List<MembershipModule> Collection()
    {
        if(!MembershipUser.Connected)
            return new List<MembershipModule>();

        return LinqUtil.Context.MembershipModules.Where(m => m.Accessible()).ToList();
    }
最佳回答

看起来像你一样,你试图执行一只lin子,反对行动,并试图把它变成一个l。

铭记: 如果听任,我预计每名成员一度会发生有关摩托路电话的q:

 return this.Privileges().Any(p => p.Name.Equals(AccessPrivilege)); 

您是否尝试利用可加利用的方法进行跳跃,并直接打败特权收集工作? 下面的法典就是这样。 我改变了返回声明。 这是一种阴性,可能不会100%正确:

public static List<MembershipModule> Collection()      
{      
    if(!MembershipUser.Connected)      
        return new List<MembershipModule>();      

    var mod = MembershipModule.SearchById(new Guid("b012d35f-6af1-47de-9e54-e5df957c07e1"));      

    var y = from p in LinqUtil.Context.MembershipModuleActions      
            where      
                p.MembershipModule.Id.Equals(mod.Id) &&      
                p.MembershipRolePrivileges.Any(rp => rp.ModuleActionId.Equals(p.Id))      
            select p.MembershipAction;      

    var u = y.Any(p => p.Name.Equals(AccessPrivilege));      

    return LinqUtil.Context.MembershipModules.Where(m => m.Privileges().Any(p => p.Name.Equals(AccessPrivilege)).ToList();  
}  
问题回答

它可以使用静态方法( 公开静态静态)。 List<_membership Module> Collection() of a group Trial Access instance members (AccessPrivilege)?





相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签