dictionary <int,guid =“”>如何获取Guid的值字段?

时间:2017-12-08 11:34:51

标签: c# asp.net linq

我为从词典中获取Guid值而苦苦挣扎。有人请帮助我。它显示System.FormatException.Thank你前进。

我的代码:

Dictionary<int, Guid> products = productRepository.QueryNoTracking()
    .Select(x => new { x.ID, x.ProductKey }) 
    .ToDictionary(x => x.ProductKey, x => x.ID);

由此我需要从位置组实体中获取productID: -

var positionGroup = new Entities.PositionGroup();

positionGroup.ProductID = (Guid?)products
   .Where(s => s.Key == productKey)
   .Select(s => s.Value).ToString();

2 个答案:

答案 0 :(得分:1)

不要使用LINQ查询来查找字典中的键/值:

Guid productID;
bool knownProductKey = products.TryGetValue(productKey, out productID);
if(knownProductKey)
    positionGroup.ProductID = productID;
else
    positionGroup.ProductID = new Nullable<Guid>();

答案 1 :(得分:1)

如果你需要帮助,你真的应该发布足够的代码,我们可以将它复制粘贴到Visual Studio&amp;很容易解决。

您提供的有限信息使得为您获取工作样本非常耗时。

无论如何,这或多或少都是其他评论的建议。

您必须根据自己的需要进行调整,因为没有足够的信息可以为您提供工作代码。

var positionGroup = new Entities.PositionGroup()
{
    ProductID = productRepository.QueryNoTracking()
        .Where(x => x.ProductKey==productKey)
        .Select(x => x.ID}) 
}