本文介绍了使用Linq从IEnumerable中选择一个表列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Sqlite的使用,我的表如下:

Am having my table as below , by the usage of Sqlite :

public class Medication
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }
    public string unique_id { get; set; }
    public string username { get; set; }
    public string insulin_type { get; set; }
    public string units { get; set; }
    public string status { get; set; }

    public string alarm_time { get; set; }
    public Medication() { }
}

现在,我想使用IEnumerable来获取列alarm_time列表,但是我不知道如何获取它.下面是我的代码:

Now I want to use IEnumerable to get a Column alarm_time List but I don't know how to get it. Below is my code for that:

public IEnumerable<Medication> AllMedicationResults()
{
    return (from t in _connection.Table<Medication>()
            select t).ToList();
}

如何在上面的代码中包含列alarm_time.

How can I include the Column alarm_time in that code above.

推荐答案

在您的select中仅请求该属性(以及更改方法的返回类型)

In your select request only that property (and change method return type)

public IEnumerable<string> AllMedicationResults()
{
    return (from t in _connection.Table<Medication>()
            select t.alarm_time).ToList();
}

但是IMO仅使用方法语法看起来会更干净:

But IMO it will look cleaner to just use method syntax:

public IEnumerable<string> AllMedicationResults()
{
    return  _connection.Table<Medication>().Select(t => t.alarm_time).ToList();
}

请注意,当您返回IEnumerable<T>时,您可能要考虑删除ToList()并利用linq延迟执行的好处

Notice that as you are returning an IEnumerable<T> you might want to consider removing the ToList() and using the benefits of linq's deffered execution

这篇关于使用Linq从IEnumerable中选择一个表列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:29