【问题标题】:Null value in List instead of a name列表中的空值而不是名称
【发布时间】:2018-06-11 21:27:28
【问题描述】:

我正在使用数据库来存储位置点的集合,并且我正在尝试创建一个消除重复项的列表。到目前为止,它部分工作。目前,当我通过

检查我的列表计数时
Debug.Log("There are " + GameManager.driftTables.Count() + " drift sets in the list.");

我得到了正确的计数。但是当我尝试通过

返回每一行的名称时
foreach (AllDrifts drift in GameManager.driftTables)
            {
                Debug.Log(drift.name);
            }

每一个我都会得到 Null。我在这里做错了什么?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using SimpleSQL;
using System.Runtime.InteropServices.ComTypes;

public class SQLiteActions : MonoBehaviour
{
    int driftCount;

    public void GetDriftTablesList()
    {
        GameManager.driftTables =
            DriftsDatabaseManager.Query<AllDrifts>(
                "SELECT DISTINCT driftID " +
                "FROM Drift"
            );

        foreach (AllDrifts drift in GameManager.driftTables)
        {
            Debug.Log(drift.name);
        }

        Debug.Log("There are " + GameManager.driftTables.Count() + " drift sets in the list.");
    }
}

public class AllDrifts
{
    [PrimaryKey]
    public string name { get; set; }
}

【问题讨论】:

  • 你认为SELECT DISTINCT driftID 是做什么的?你认为它返回name 吗?或者只是driftID
  • 我以为它会返回名字
  • 好的,对,这就是我的问题。我正在存储 GameManager.driftTables 中存储的每个不同漂移的名称,并将其设置为 null,而我想要做的是将 GameManager.driftTables 中每个列表项的名称设置为与从中找到的相同的不同名称查询“SELECT DISTINCTdriftID FROM Drift”
  • 您需要将driftID更改为您的姓名列的名称。
  • "到从查询 SELECT DISTINCT driftID FROM Drift 中找到的相同的不同名称。这与从{ red, green, blue } 中选择dog 有点相同。

标签: c# list sqlite unity3d


【解决方案1】:

我解决了这个问题,不再使用 DISTINCT,而是只找到 DriftIndex 值第一次出现在列表中,然后像这样丢弃所有重复项。

using UnityEngine;
using System.Linq;
using SimpleSQL;

    public class DriftSets
    {
        [PrimaryKey, AutoIncrement]
        public int DriftIndex { get; set; }
        public string DriftID { get; set; }
        public string DriftDateTime { get; set; }
        public string DriftName { get; set; }
        public string DriftStep { get; set; }
        public float DriftLat { get; set; }
        public float DriftLong { get; set; }
        public string DriftTexLocation { get; set; }
    }

    public class SQLiteActions : MonoBehaviour
    {
        public static SQLiteActions instance = null;
        // reference to the database manager in the scene
        public SimpleSQLManager DriftsDatabaseManager;

        private string sql;

        public void GetDriftTablesList()
        {
            sql =
                "SELECT MIN(DriftIndex), DriftID, DriftName, DriftDateTime, DriftTexLocation  " +
                "FROM Drift " +
                "GROUP BY DriftID " +
                "ORDER BY DriftDateTime DESC";

            GameManager.driftSets = DriftsDatabaseManager.Query<DriftSets>(sql);

            foreach (var driftSet in GameManager.driftSets)
            {
                Debug.Log(driftSet.DriftName);
            }
        }
     }

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多