简体   繁体   中英

dictionary<int, guid> how to take value field that is Guid?

I was struggling a lot to get a Guid Value from Dictionary. Somebody please help me out.It is showing System.FormatException.Thank you advance.

My code:

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

From this I need to take productID from Position group Entity:-

var positionGroup = new Entities.PositionGroup();

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

Don't use a LINQ query to find the key/value in a dictionary:

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

If you want help you really should post enough code that we can just copy-paste it into Visual Studio & fix it easily.

The limited info you've given makes it really time consuming to get a working sample for you.

In any case, this is more or less what the other comments are suggesting.

You will have to get it adjust for your needs because there's not enough info to give you working code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM