Spoulson has it nearly right, but you need to create a List from string first. Actually a List would be better if uid is also int. List supports Contains().
Doing uid.ToString(). Contains( string ) would imply that the uid as a string contains all of the values of the array as a substring? Even if you did write the extension method the sense of it would be wrong EDIT Unless you changed it around and wrote it for string as Mitch Wheat demonstrates, then you'd just be able to skip the conversion step ENDEDIT Here is what you want, if you don't do the extension method (unless you already have the collection of potential uids as ints -- then just use List() instead) List uids = new List( arrayofuids ); from xx in table where uids.
Contains( xx.uid.ToString() ) select xx.
Spoulson has it nearly right, but you need to create a List from string first. Actually a List would be better if uid is also int. List supports Contains().
Doing uid.ToString(). Contains( string ) would imply that the uid as a string contains all of the values of the array as a substring? Even if you did write the extension method the sense of it would be wrong.
EDIT Unless you changed it around and wrote it for string as Mitch Wheat demonstrates, then you'd just be able to skip the conversion step. ENDEDIT Here is what you want, if you don't do the extension method (unless you already have the collection of potential uids as ints -- then just use List() instead). List uids = new List( arrayofuids ); from xx in table where uids.
Contains( xx.uid.ToString() ) select xx.
Thank you. It was the right answer... One more thought? Lets say the arrayuids is also a linq query.
Any way you could get both statements down to just one query from the database? – Scott Oct 12 '08 at 21:07 According to MSDN, string implements IEnumerable, which has a Contains method. Therefore, it's not necessary to convert the array to an IList.Msdn.microsoft.Com/en-us/library/19e6zeyy.
Aspx – spoulson Oct 13 '08 at 1:11 See code example: rafb. Net/p/AMsTaB42. Html – spoulson Oct 13 '08 at 1:40 Correction, here's the code link: pastebin.
Com/f30868a36 – spoulson Oct 14 '08 at 19:43 The last .ToString() throws errors for me. Specifically, LINQ to Entities does not recognize the method 'System. String ToString()' method, and this method cannot be translated into a store expression.... After removing it, the lambda worked for me.
– Sam Stange Sep 21 at 15:00.
If you are truly looking to replicate Contains, but for an array, here is an extension method and sample code for usage: using System; using System.Collections. Generic; using System. Linq; using System.
Text; namespace ContainsAnyThingy { class Program { static void Main(string args) { string testValue = "123345789"; //will print true Console. WriteLine(testValue. ContainsAny("123", "987", "554")); //but so will this also print true Console.
WriteLine(testValue. ContainsAny("1", "987", "554")); Console.ReadKey(); } } public static class StringExtensions { public static bool ContainsAny(this string str, params string values) { if (!string. IsNullOrEmpty(str) || values.
Length > 0) { foreach (string value in values) { if(str. Contains(value)) return true; } } return false; } } }.
1 @Jason, you should totally submit this to ExtensionMethod. Net Thanks for the great code, it solved my problem today! – p.
Campbell Nov 19 '09 at 18:33 esto es oro en polvo! – Luiscencio Mar 25 '10 at 18:36 1 I think you meant!string. IsNullOrEmpty(str) && values.
Length > 0 – gbogumil Jun 18 '10 at 19:03 You are right. I changed it, though it doesn't have a functional impact. I use a function like this at work.
I will have to check it! – Jason Jackson Jun 18 '10 at 23:58.
Try the following. String input = "someString"; string toSearchFor = GetSearchStrings(); var containsAll = toSearchFor. All(x => input.
Contains(x)).
2 I really wish people would leave a comment when they mark you down. Especially since the answer I provided is 100% correct. – JaredPar Oct 13 '08 at 4:47 It wasn't me, but doesn't All() return simply a bool indicating where all items match the condition?
And initializing toSearchFor to null guarantess a NullReferenceException. – Lucas Oct 13 '08 at 22:46 I editted the null issue to be what I intended to type. Yes on All.
This effectively ensures that all strings in toSearchFor are contained within the input string. – JaredPar Oct 15 '08 at 8:33.
How about: from xx in table where stringarray. Contains(xx.uid.ToString()) select xx.
NotSupportedException: Comparison operators not supported for type 'System. String' Thanks but try again? – Scott Oct 12 '08 at 1:31 This works ok for me.
See this code example: rafb. Net/p/AMsTaB42. Html – spoulson Oct 13 '08 at 1:39 +1, if this is actually what they want.It's not very clear from the question.
– Lucas Oct 14 '08 at 18:24 Correction, here's the code link: pastebin. Com/f30868a36 – spoulson Oct 14 '08 at 19:43.
This is an example of one way of writing an extension method (note: I wouldn't use this for very large arrays; another data structure would be more appropriate...): namespace StringExtensionMethods { public static class StringExtension { public static bool Contains(this string stringarray, string pat) { bool result = false; foreach (string s in stringarray) { if (s == pat) { result = true; break; } } return result; } } }.
1 that would be identical to public static bool Contains(this string stringarray, string pat) { return Array. IndexOf(stringarray, pat)! = -1; } – James Curran Oct 12 '08 at 2:52 4 string implements IEnumerable, so it already has a Contains(string) extension method.
Why are we reimplementing this? – Lucas Oct 13 '08 at 22:50.
Or if you already have the data in a list and prefer the other Linq format :) List uids = new List(){"1", "45", "20", "10"}; List table = GetDataFromSomewhere(); List newTable = table. Where(xx => uids. Contains(xx.
Uid)).ToList().
I believe you could also do something like this. From xx in table where (from yy in string select yy). Contains(xx.uid.ToString()) select xx.
Same as "where stringArray. Contains(xx.uid.ToString())", no need to wrap it around in a query – Lucas Oct 13 '08 at 22:51.
If this is true you may want to really rethink this whole approach, this seems like a really bad idea. You should probably be trying to match a Guid to a Guid Guid id = new Guid(uid); var query = from xx in table where xx. Uid == id select xx; I honestly can't imagine a scenario where matching a string array using "contains" to the contents of a Guid would be a good idea.
For one thing, Contains() will not guarantee the order of numbers in the Guid so you could potentially match multiple items. Not to mention comparing guids this way would be way slower than just doing it directly.
You should write it the other way around, checking your priviliged user id list contains the id on that row of table: string search = new string { "2", "3" }; var result = from x in xx where search. Contains(x.uid.ToString()) select x; LINQ behaves quite bright here and converts it to a good SQL statement: sp_executesql N'SELECT t0. Uid FROM dbo.
Xx AS t0 WHERE (CONVERT(NVarChar,t0. Uid)) IN (@p0, @p1)',N'@p0 nvarchar(1), @p1 nvarchar(1)',@p0=N'2',@p1=N'3' which basicly embeds the contents of the 'search' array into the sql query, and does the filtering with 'IN' keyword in SQL.
I managed to find a solution, but not a great one as it requires using AsEnumerable() which is going to return all results from the DB, fortunately I only have 1k records in the table so it isn't really noticable, but here goes. Var users = from you in (from you in ctx. Users where u.
Mod_Status! = "D" select u).AsEnumerable() where ar. All(n => u.FullName.
IndexOf(n, StringComparison. InvariantCultureIgnoreCase) >= 0) select u; My original post follows: How do you do the reverse? I want to do something like the following in entity framework.
String search = new string { "John", "Doe" }; var users = from you in ctx. Users from s in search where u.FullName. Contains(s) select u; What I want is to find all users where their FullName contains all of the elements in `search'.
I've tried a number of different ways, all of which haven't been working for me. I've also tried var users = from you in ctx. Users select u; foreach (string s in search) { users = users.
Where(u => u.FullName. Contains(s)); } This version only finds those that contain the last element in the search array.
The best solution I found was to go ahead and create a Table-Valued Function in SQL that produces the results, such as :: CREATE function dbo. GetMatches(@textStr nvarchar(50)) returns @MatchTbl table( Fullname nvarchar(50) null, ID nvarchar(50) null ) as begin declare @SearchStr nvarchar(50); set @SearchStr = '%' + @textStr + '%'; insert into @MatchTbl select (LName + ', ' + FName + ' ' + MName) AS FullName, ID = ID from employees where LName like @SearchStr; return; end GO select * from dbo. GetMatches('j') Then, you simply drag the function into your LINQ.
Dbml designer and call it like you do your other objects. The LINQ even knows the columns of your stored function. I call it out like this :: Dim db As New NobleLINQ Dim LNameSearch As String = txt_searchLName.
Text Dim hlink As HyperLink For Each ee In db. GetMatches(LNameSearch) hlink = New HyperLink With {. Text = ee.
Fullname & "", . NavigateUrl = "? ID=" & ee.ID} pnl_results.Controls.
Add(hlink) Next Incredibly simple and really utlizes the power of SQL and LINQ in the application...and you can, of course, generate any table valued function you want for the same effects!
I believe that what you really want to do is: let's imagine a scenario you have two database and they have a table of products in common And you want to select products from the table "A" that id has in common with the "B" using the method contains would be too complicated to do this what we are doing is an intersection, and there is a method called intersection for that an example from msdn: msdn.microsoft.com/en-us/vcsharp/aa33676... int numbers = (0, 2, 4, 5, 6, 8, 9); int numbersB = (1, 3, 5, 7, 8); var = commonNumbers numbersA. Intersect (numbersB); I think what you need is easily solved with intersection.
String stringArray = {1,45,20,10}; from xx in table where stringArray. Contains(xx.uid.ToString()) select xx.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.