views:

220

answers:

4

Hi there,

Can anyone help?

I have a linq query which is embedded inside a extension method, it was working as v.RentalStatus was a String. I am now using a Group on my original query (the query is quite complex so i won't put it here).

The importante thing is that v.RentalStatus = IEnumerable hence it can contain things like

A (meaning active)
R (meaning rented)
U (unavailable)

etc - many more

I create a list of what i would like to get back and store this in statusStringList, so for example lets say the list contains A and R

This is my code from before when the v.RentalStatus was just a string, can anyone tell me how i can modify this to work.

        var statusStringList = rentalStatus.ToList().ConvertAll<string>(st => st.GetStringValue());

        return from v in qry
               where statusStringList.Contains(v.RentalStatus)
               select v;

If it helps this is part of my query which returns the RentalStatus - its part of a group query but the RentalStatus is not in the group by

 RentalStatus= g1.Select( j => j.IdRentalStatus).Distinct(),

g1 is my group by, so if you imagine there are 10 "A", 5 "U" .. then it would return an ienumerable of A and U ... as i am using Distinct. Not 10 As and 5 Us

I hope i have explained it well, please tell me if i haven't

I would appreciate any help from anyone ..

thanks

EDIT

This is my extension signature but not that it matters.

    public static IQueryable<Rentals> WithStatus(this IQueryable<Rentals> qry, IList<Contants.Statuses> rentalStatus)
    {

EDIT

As mentioned previously when v.RentalStatus was a string it was working but now its IEnumerable - hence a collection.. and it errors with this

  Argument '1': cannot convert from 'System.Collections.Generic.IEnumerable<string>' to 'string'
A: 

I am not sure but I think that for a Contains to work in Linq to SQL it must be an array of strings (or ints or ...) and not any IEnumerable. I would thus try:

var statusStringArray = rentalStatus.ToList().ConvertAll<string>(st => st.GetStringValue()).ToArray();

return from v in qry
       where statusStringArray.Contains(v.RentalStatus)
       select v;

There might be other issues though, I did not look that much.

Marcel Gosselin
Thanks, but it was working before, i should explain that it is erroring on v.RentalStatus.. I will update my question with a bit more details.. basicalyl before v.RentalStatus was a STRING .. and it worked perfectly but now its a collection of strings i.e. Ienumerable<string>
mark smith
question has been updated
mark smith
+1  A: 

You could try something like this:

where statusStringList.Any(x => v.RentalStatus.Contains(x))
John Fisher
Thanks John, tried it but it returns "Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator."
mark smith
ah i just noticed that J.13.L answer works but the difference is the order of the statusStringList and RentalStatus - they are swapped around
mark smith
A: 

Try this:

    return from v in qry
           where rentalStatus.Any( r => r.IdRentalStatus == v.RentalStatus)
           select v;
Gabe Moothart
+1  A: 

If RentalStatus has changed from a string to a IEnumberable then your comparing 2 list... I think this should work:

return from v in qry
    where v.RentalStatus.Any(status => statusStringList.Contains(status))
    select v;

This should give you any rentals that have a status that is in the list you are providing

Edit:

Yeah I would spend some time learn lambda expressions. Seems like they are being used more and more and with good reason. Here are a few links for tutorials:

An Extensive Examination of LINQ: Lambda Expressions and Anonymous Types

.NET Lambda Expressions – Resources

"WHERE" RentalStatus = Containing any of itself - arrgghh -

Is that true? I thought the list of rentalStatuses is a parameter in your method. I was thinking your query basically would allow me to get all the rentals that have a status that matches any of the list that I specified. One list lives on your Rental object and the other is the one I pass in...

As to why the order in mine worked. I have some questions:

Are you using this to query a database? Are you able to look at the tsql it generates?

If so, I would look at the tsql and see what the difference is. I would have to check myself. I got lucky I guess.

J.13.L
Thanks J.13.L, it seems to have worked! thank you. I would like to know why :-) ... Can you explain what is actually happening? .. basically it says Return "WHERE" RentalStatus = Containing any of itself - arrgghh - I would love to know a good explanation... can you elabortate? I feel that i will be using more of these in the future and i am ok with the normal lambdas but still a novice and this went over my head a bit :-)
mark smith
I made some changes...
J.13.L
Hi thanks for the reply, yes indeed the RentalStatus is retruned from the db as Ienumerable<string> ... it works great.... Thanks once again..
mark smith