我制作了一个字典来收集一些数据。从字典中提取我想要的所以我想使用LINQ。但是我不知道该怎么做
我想通过LINQ从字典中提取数据并存储为列表
来自textDataDictionary中的textPoint1X和textPointY
哪里,范围?
:2350 // class for dictionary
class textDataType
{
public string text { get; set; }
public double textPoint1X { get; set; }
public double textPoint1Y { get; set; }
}
// declare a dictionary
var textDataDictionary = new Dictionary<UInt64, textDataType>();
// add data
textDataDictionary.Add(textData.Handle,
new textDataType
{
text = textData.Text,
textPoint1X = textData.Point1.X,
textPoint1Y = textData.Point1.Y
});
答案 0 :(得分:3)
这里有一些LINQ可以帮助您。您需要在字典的def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
datadir = os.path.dirname(__file__)
# The following line has been changed to match where you store your data files:
return os.path.join(datadir, 'Resources', filename)
上使用Where
进行过滤(以获得Values
),然后最终将其转换为所需的列表。
ValueCollection
答案 1 :(得分:2)
您可以使用字典Values
属性,并在其上使用Linq Where()
(下面的示例)
textDataDictionary.Values.Where(x => x.textPoint1X > 2350 && x.textPoint1X < 2355).ToList();
答案 2 :(得分:0)
以下是语法形式的linq查询:
var query =
from p in textDataDictionary
let x = p.Value.textPoint1X
let y = p.Value.textPoint1Y
where 2350 < x && x < 2355 && -300 < y && y < -298
select p.Value;
var result = query.ToList();