Confused about conversion


S

shapper

Hello,

I have the following Linq query:

bool a = (from t in database.Tags
where t.Name == Name
select t).Any();

A equivalent lambda expression would be:
bool a = database.Tags.Where(t => t.Name == this.Tag.Name).Any()

or

bool a = database.Tags.Select(t => t.Name == this.Tag.Name).Any()

Thanks,
Miguel
 
Ad

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

Miguel,

What is the question? Are you asking if those calls will produce the
same results? We couldn't tell you the answer, because the first does the
following comparison:

t.Name == Name

While the second two compares the following:

t.Name == this.Tag.Name

So technically, it's impossible to say not knowing what Name is vs.
this.Tag.Name.

That being said, if the comparison of t.Name was against the same thing
in each example, then yes, all three will have the same result.
 
S

shapper

Miguel,

    What is the question?  Are you asking if those calls will produce the
same results?  We couldn't tell you the answer, because the first does the
following comparison:

t.Name == Name

    While the second two compares the following:

t.Name == this.Tag.Name

    So technically, it's impossible to say not knowing what Name is vs.
this.Tag.Name.

    That being said, if the comparison of t.Name was against the samething
in each example, then yes, all three will have the same result.

--
          - Nicholas Paldino [.NET/C# MVP]
          - (e-mail address removed)


I have the following Linq query:
  bool a = (from t in database.Tags
                where t.Name == Name
                select t).Any();
A equivalent lambda expression would be:
bool a = database.Tags.Where(t => t.Name == this.Tag.Name).Any()

bool a = database.Tags.Select(t => t.Name == this.Tag.Name).Any()
Thanks,
Miguel
Sorry, yes it is:

bool a = (from t in database.Tags
where t.Name == t.Tag.Name
select t).Any();

Error when typing.

So should I use Select or Where in this case?

I think when the lambda expression is not returning a list but a bool,
int, etc I can use either Where or Select. Is that it?

Thanks,
Miguel
 
Ad

Advertisements

N

Nicholas Paldino [.NET/C# MVP]

Miguel,

I am sorry, I was mistaken. Select and Where will NOT do the same thing
here.

When you call select, it will return an IEnumerable<bool> and not return
a filtered sequence.

You want to call Select for the purpose of projections (what you want
the shape of the final result to be), and Where when you want to do
filtering. Use Where in this case.

I was mistaken because there are other places in LINQ that take
Predicate<T> instances to filter results and I assumed this was one of them.

Any is one of those methods. You could do this in your original
example:

bool a = database.Tags.Any(t => t.Name == this.Tag.Name);

But if you are running against a database, I don't know that Any will
translate to the underlying database (it very well could). If not, then you
can always use Where.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Miguel,

What is the question? Are you asking if those calls will produce the
same results? We couldn't tell you the answer, because the first does the
following comparison:

t.Name == Name

While the second two compares the following:

t.Name == this.Tag.Name

So technically, it's impossible to say not knowing what Name is vs.
this.Tag.Name.

That being said, if the comparison of t.Name was against the same thing
in each example, then yes, all three will have the same result.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I have the following Linq query:
bool a = (from t in database.Tags
where t.Name == Name
select t).Any();
A equivalent lambda expression would be:
bool a = database.Tags.Where(t => t.Name == this.Tag.Name).Any()

bool a = database.Tags.Select(t => t.Name == this.Tag.Name).Any()
Thanks,
Miguel
Sorry, yes it is:

bool a = (from t in database.Tags
where t.Name == t.Tag.Name
select t).Any();

Error when typing.

So should I use Select or Where in this case?

I think when the lambda expression is not returning a list but a bool,
int, etc I can use either Where or Select. Is that it?

Thanks,
Miguel
 

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

Top