Compare

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hi,

In a Linq query I have the following:

where t.Name == tag

How can I make this line to have no effect if tag is empty?
In this moment if tag is empty I don't get any record.

tag is a string.

Thank You,
Miguel
 
Hi,

In a Linq query I have the following:

where t.Name == tag

How can I make this line to have no effect if tag is empty?
In this moment if tag is empty I don't get any record.

tag is a string.

Thank You,
Miguel

What do you mean when you say, "tag is empty"? How is tag defined?
Is it of type System.Object? or is it a string?

You could try using something like:

where t.Name == tag || tag == null //Or whatever is appropriate
for an "empty" tag.

Not sure if this is what you need.

Chris
 
What do you mean when you say, "tag is empty"?  How is tag defined?
Is it of type System.Object? or is it a string?

You could try using something like:

where t.Name == tag || tag == null        //Or whatever is appropriate
for an "empty" tag.

Not sure if this is what you need.

Chris

The query is inside a function:
public ActionResult List(int? page, string tag) {

I think I could, for example, call List(null, "")

I am not sure if I can call List(null, null)

I tried to use:
public ActionResult List(int? page, string? tag)

But I get the error:
The type 'string' must be a non-nullable value type in order to use it
as parameter 'T' in the generic type or method 'System.Nullable<T>'

Thanks,
Miguel
 
The query is inside a function:
public ActionResult List(int? page, string tag) {

I think I could, for example, call List(null, "")

I am not sure if I can call List(null, null)

I tried to use:
public ActionResult List(int? page, string? tag)

But I get the error:
The type 'string' must be a non-nullable value type in order to use it
as parameter 'T' in the generic type or method 'System.Nullable<T>'

Thanks,
Miguel

That's because string is already nullable, it makes no sense to
declare it as string?.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Linq. Select 1
Contains Error 1
List 2
Adobe "Wrong type parameter supplied to a PDS procedure" 0
(Collection) 1
Local Sequence cannot be used ... except the Contains() operator 4
Linq and JSon 5
Linq > Group 2

Back
Top