简体   繁体   English

如何在C#中为linq设置值?

[英]How to set a value into a linq in c#?

I am very new with lambda expressions. 我对lambda表达式非常陌生。 I want to have the count of all the status_ID who has the number 6. Then I want to convert this as a list. 我想拥有所有编号为6的status_ID的计数。然后,我要将其转换为列表。 What is the right syntax to set a value to the query? 为查询设置值的正确语法是什么? I know it in SQL but not in lambda expressions. 我知道在SQL中,但在lambda表达式中却不知道。

return ContextHelperSlowly<Moi>.GetCount(false, x => x.Status_ID == 6).ToList();

Kind regards 亲切的问候

Update 更新资料

  public static int GetCount(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties)
    {
        int list;
        using (var context = GetContext())
        {
            IQueryable<T> dbQuery = context.Set<T>();
            if (null != navigationProperties)
            {
                foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
                {
                    dbQuery = dbQuery.Include(navigationProperty);
                }
            }
            if (null == where)
            {
                where = arg => true;
            }
            list = dbQuery
                .Where(x => x.Date_Deleted == null && x.End_Validity == null)
                .Where(where)
                .Count();
        }
        return list;
    }

I would just change your GetCount method to GetList: 我只是将您的GetCount方法更改为GetList:

public static List<T> GetList(Func<T, bool> where, params Expression<Func<T, object>>[] navigationProperties)
{
    using (var context = GetContext())
    {
        IQueryable<T> dbQuery = context.Set<T>();
        if (null != navigationProperties)
        {
            foreach (Expression<Func<T, object>> navigationProperty in navigationProperties)
            {
                dbQuery = dbQuery.Include(navigationProperty);
            }
        }
        if (null == where)
        {
            where = arg => true;
        }
        return dbQuery
            .Where(x => x.Date_Deleted == null 
                     && x.End_Validity == null
                     && where(x))
            .ToList();
    }
}

And use: 并使用:

return ContextHelperSlowly<Moi>.GetList(false, x => x.Status_ID == 6);

To get the count as an int: 要将计数作为整数:

return ContextHelperSlowly<Moi>.GetList(false, x => x.Status_ID == 6).Count;

Why do you want to write those functions yourself? 为什么要自己编写这些功能?

this gives you the number of items with Status_ID = 6: 这将为您提供Status_ID = 6的项目数:

yourList.Count(x => x.Status_ID == 6);

and this gives you a list of all entries with Status_ID = 6: 这为您提供了Status_ID = 6的所有条目的列表:

yourList.Where(x => x.Status_ID == 6).ToList();

And a little notice for "code style". 还有一点关于“代码样式”的注意事项。 You can do this however you want, but C# standard would be "StatusId" for a property. 您可以根据需要执行此操作,但是C#标准将是属性的“ StatusId”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM