1

I am getting this error in a LINQ query with the below code:

"Operator '??' cannot be applied to operands of type 'System.Guid?' and 'string'"

I have a nullable GUID object, here I am checking if the GUID Object is null, then I need to return String.Empty else will be converting GUID to string

List<Report> queryresult =
    reportDatatable.AsEnumerable()
        .Where(c => c.Field<Guid?>("ParentID") == Guid.Parse("XXXXXX-XXX-XXXXX-XXXXX-XXX))
        .Select(c=> new Report()
        {
            ID = (c.Field<Guid?>("ID") ?? String.Empty).ToString(), //error here
            ...
        }).ToList();

Below link as simplest way to check whether object is null or not.

Suggest if any other approach to check GUID object is null or not

Ian
  • 30,182
  • 19
  • 69
  • 107
Kalidoss M
  • 556
  • 8
  • 15

4 Answers4

4

Edit:

Apparently, what suits your need best would be this:

ID = (c.Field<Guid?>("ID") ?? Guid.Empty) == Guid.Empty ? String.Empty : c.Field<Guid?>("ID").ToString(), //try this

Original

Convert your GUID to string first:

c.Field<Guid?>("ID").ToString() ?? String.Empty

Then it should be ok.

Else, use Guid.Empty instead of String.Empty the idea here is that both sides in ?? must match. Like comparing Apple to Apple

To prevent c.Field<Guid?>("ID") == null being executed, in C#6, you could make it more simply like this

c.Field<Guid?>("ID")?.ToString() ?? String.Empty
Ian
  • 30,182
  • 19
  • 69
  • 107
  • 2
    `ToString()` never returns `null`, making this not very useful. –  Jan 29 '16 at 06:43
  • If `c.Field("ID")` was null in the first place, calling a method on it isn't going to end well. You could replace the dot with `?.` to make this work. – chris Jan 29 '16 at 06:45
  • Make that `c.Field("ID")?.ToString() ?? String.Empty` (if using C# 6) – Henrik Ilgen Jan 29 '16 at 06:45
  • @hvd String.Empty isn't null I think... http://stackoverflow.com/questions/6689876/string-empty-vs-null-which-one-do-you-use – Ian Jan 29 '16 at 06:45
  • @Ian, Nope, String.Empty isn't null, but `c.Field("ID")` might be – Henrik Ilgen Jan 29 '16 at 06:46
  • @Ian If the LHS of `??` is never `null`, the `??` operator doesn't make sense. –  Jan 29 '16 at 06:49
  • @HenrikIlgen ok, I updated the final answer according to the need. It seems like what OP wants involves **two** steps: (1) to check Guid, (2) To convert into `string` – Ian Jan 29 '16 at 07:09
  • Er... I meant something else. I meant that just `c.Field("ID").ToString()` would be enough. `((Guid?)null).ToString()` produces an empty string. :) –  Jan 29 '16 at 08:16
  • @hvd "`((Guid?)null).ToString()` produces an `Empty.String`" this is really new for me.. :s you should be the answerer, Sir. Not me... Or I should just put it but credit it to you/any of your answers? I think it is fairer that way... :s – Ian Jan 29 '16 at 08:19
  • Thanks :) Given your initial version of that last comment I started typing up an answer, and I didn't see your edit before I posted it. –  Jan 29 '16 at 08:25
1

Given that Nullable<>'s ToString() override produces an empty string if the value is null, you can simply write

ID = c.Field<Guid?>("ID").ToString()

Depending on your reporting code, better yet could be to make Report's ID of type Guid?, but that won't necessarily always work.

  • Ok, voted up. ;) I will tell the OP to uncheck my answer and check yours too. ;) – Ian Jan 29 '16 at 08:26
  • Ok, I already put up a comment in about your answer in the OP post, hope he will notice. ;) again thanks for the new stuffs I learn from you. ;) It just surprise me that nullable type, when the value is null can use `ToString()` method like that... – Ian Jan 29 '16 at 08:28
0

You need to return Guid.Empty instead of String.Empty for ?? operator as left hand side is Guid not string. That is what the compiler error "Operator '??' cannot be applied to operands of type 'System.Guid?' and 'string'" suggests.

 ID = (c.Field<Guid?>("ID") ?? Guid.Empty).ToString(),

Edit

If you want empty string then you can put condition to return empty string for Guid.Empty. Your query will end up with something like this.

ID = (c.Field<Guid?>("ID") ?? Guid.Empty) == Guid.Empty ? 
        String.Empty : c.Field<Guid?>("ID").ToString(),
Adil
  • 146,340
  • 25
  • 209
  • 204
  • This would return the empty GUID (`00000000-0000-0000-0000-000000000000`) in case the field is null, which is not what OP needs to return: *if the GUID Object is null the i need to return String.Empty* – Henrik Ilgen Jan 29 '16 at 06:56
  • It required OP to adjust the code accordingly but he can return empty string, check updated answer. Thanks for pointing though. – Adil Jan 29 '16 at 07:10
0

As you have nullable GUID, you can use HasValue to check if value exist

(c.Field<Guid?>("ID").HasValue ? c.Field<Guid?>("ID").Value:String.Empty).ToString()
Viru
  • 2,228
  • 2
  • 17
  • 28