Compare

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
 
C

Chris Dunaway

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
 
S

shapper

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
 
C

Chris Dunaway

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
Local Sequence cannot be used ... except the Contains() operator 4
Condition 2
Linq Query. Please, help. Going crazy ... 1
Filter 1
List 2
ToString and FromString 10

Top