【问题标题】:Simple SELECT WHERE LINQ Query to a list简单的 SELECT WHERE LINQ 查询到列表
【发布时间】:2015-07-16 22:04:32
【问题描述】:

我是 LINQ 新手,

我想从我的 dbcontext 中生成一个对象列表,其中某个字段设置为 true。

这是我目前所拥有的,但我收到关于选择的错误?

using (var db = new dbContext())
{
    return (from s in db.sims.Where(x=>x.has_been_modified == true) select x).ToList();               
}

编辑:

    //Returns a list of entries which where marked as edited in the sim managment database
    private List<String> GetUpdatedEntries()
    {
        using (var db = new dbContext())
        {
            return db.sims.Where(x => x.has_been_modified).ToList();                  
        }
    }

【问题讨论】:

    标签: c# linq entity-framework


    【解决方案1】:

    select s,而不是x,这将起作用。 (因为你做from s

    短途

    return db.sims.Where(x => x.has_been_modified).ToList();
    

    供您编辑

    方法返回类型应该是List&lt;Sim&gt;,而不是List&lt;String&gt;

    【讨论】:

    • 我的方法应该返回什么类型的列表才能使其正常工作?请参阅我的编辑。我知道我有 list 但它应该是什么类型?
    • @Zapnologica 应该是 List&lt;Sim&gt;(或类似的东西)。
    【解决方案2】:

    这会起作用

    return db.sims.Where(x=>x.has_been_modified).ToList();
    
    1. 方法 Linq 在这里看起来更干净
    2. 您无需检查您的布尔值是否为真
    3. 在您之前的回答中,您使用s 作为上下文并选择了x,更改为select s 也应该可以工作
    4. 考虑使用延迟加载,不要在每个查询末尾添加ToList

    【讨论】:

    猜你喜欢
    • 2012-02-05
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多