English 中文(简体)
LINQ: Help with linq query and contains for an IEnumerable<string>?
原标题:

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 
最佳回答

If RentalStatus has changed from a string to a IEnumerable<string> 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.

问题回答

You could try something like this:

where statusStringList.Any(x => v.RentalStatus.Contains(x))

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.

Try this:

    return from v in qry
           where rentalStatus.Any( r => r.IdRentalStatus == v.RentalStatus)
           select v;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签