Filtering array in monoTouch with NSPredicate - c#

So I'm trying to filter an array with NSPredicate in MonoTouch. In Objective-c it would be something like this:
NSPredicate *findStringWithReference = [NSPredicate predicateWithFormat:#"SELF CONTAINS [cd] %#",cRText.text];
NSArray *trackTraceContentFiltered = [trackTraceContent filteredArrayUsingPredicate:findStringWithReference];
I can't figure out how to do this in c#.

Do you have to use an NSArray or could you use a .Net List / Collection enabling you to use Linq?
101 LINQ Samples is a great resource for using LINQ.

Something like this:
NSPredicate findStringWithReference = NSPredicate.FromFormat("SELF CONTAINS [cd] %#", new NSObject[] {cRText.StringValue } );
var trackTraceContentFiltered = trackTraceContent.Filter(findStringWithReference);
If you are working in .NET, you can use LINQ. There are lots of examples online, something like this:
var array = new string[] { "one", "two", "three" };
var filteredResult = array.Where(x => x.Contains("ne");

Related

Can I disable stemming / stop words filtering with MongoDB 4 text search, in C#

I would like to use the C# driver in MongoDB to make a full text search.
But I see that when I create the index, I can't select 'none' as a language.
I would like terms to be matched as they are and without removing the stop words either.
Given a type
public class Entity
{
public string Text;
}
You can do this:
var collection = new MongoClient().GetDatabase("test").GetCollection<Entity>("collection");
var indexKeysDefinition = new IndexKeysDefinitionBuilder<Entity>().Text(x => x.Text);
var createIndexOptions = new CreateIndexOptions { DefaultLanguage= "none" };
collection.Indexes.CreateOne(new CreateIndexModel<Entity>(indexKeysDefinition, createIndexOptions));

Combine 2 lists on items where string matches between lists

I have 2 List<string>s that contain a list of network names.
List<string> nets1 = new List<string>() { "net1", "net2", "net3" };
List<string> nets2 = new List<string>() { "net2", "net3", "net4" };
I want to combine them into a new List<string>, but only where the strings are equal. So my desired result will be of type List<string> and contain ONLY net2 and net3.
I have tried to use Union and Concat but they dont seem to be what I am looking for
What you are looking for is Intersect:
var list = list1.Intersect(list2).ToList();
Hope you are looking for the common elements in both list, you can use Intersect
var commonElements = nets1.Intersect(nets2).ToList();

How to sample a list of doubles in c#

Suppose I have a list of double values in C# that contains a pair of double values that looks like this:
var ListToSample = new List<double>
{ 49.5353, 49.6241, 49.92432 };
And from these values I want to create a 100 samples. What is the most effective way of doing that.
You could use Enumerable.Repeat:
var hundredSamples = Enumerable.Repeat(ListToSample, 100);
var allLists = new List<List<Double>>(100);
allLists.AddRange(hundredSamples);
or in one line:
var allLists = Enumerable.Repeat(ListToSample, 100).ToList();

Remove duplicate items from a List<String[]> in C#

I have an issue here a bit complex than I'm trying to resolve since some days ago. I'm using the PetaPoco ORM and didn't found any other way to do a complex query like this:
var data = new List<string[]>();
var db = new Database(connectionString);
var memberCapabilities = db.Fetch<dynamic>(Sql.Builder
.Select(#"c.column_name
,CASE WHEN c.is_only_view = 1
THEN c.is_only_view
ELSE mc.is_only_view end as is_only_view")
.From("capabilities c")
.Append("JOIN members_capabilities mc ON c.capability_id = mc.capability_id")
.Where("mc.member_id = #0", memberID)
.Where("c.table_id = #0", tableID));
var roleCapabilities = db.Fetch<dynamic>(Sql.Builder
.Select(#"c.column_name
,CASE WHEN c.is_only_view = 1
THEN c.is_only_view
ELSE rc.is_only_view end as is_only_view")
.From("capabilities c")
.Append("JOIN roles_capabilities rc ON c.capability_id = rc.capability_id")
.Append("JOIN members_roles mr ON rc.role_id = mr.role_id")
.Where("mr.member_id = #0", memberID)
.Where("c.table_id = #0", tableID));
I'm trying to get the user capabilities, but my system have actually to ways to assign an user a capability, or direct to that user or attaching the user to a role. I wanted to get this merged list using a stored procedure but I needed cursors and I thought maybe should be easier and faster doing this on the web application. So I get that two dynamics and the members capabilities have priority to the roles capabilities, so I need to check if that using loops. And I did like this:
for (int i = 0; i < roleCapabilities.Count; i++)
{
bool added = false;
for (int j = 0; j < memberCapabilities.Count; j++)
if (roleCapabilities[i].column_name == memberCapabilities[j].column_name)
{
data.Add(new string[2] { memberCapabilities[j].column_name, Convert.ToString(memberCapabilities[j].is_only_view) });
added = true;
break;
}
if (!added)
data.Add(new string[2] { roleCapabilities[i].column_name, Convert.ToString(roleCapabilities[i].is_only_view) });
}
So now the plan is delete the duplicate entries. I have try using the following methods with no results:
data = data.Distinct();
Any help? Thanks
Make sure that your object either implements System.IEquatable or overrides Object.Equals and Object.GetHashCode. In this case, it looks like you're storing the data as string[2], which won't give you the desired behavior. Create a custom object to hold the data, and do one of the 2 options listed above.
If I understand your question correctly you want to get a distinct set of arrays of strings, so if the same array exists twice, you only want one of them? The following code will return arrays one and three while two is removed as it is the same as one.
var one = new[] {"One", "Two"};
var two = new[] {"One", "Two"};
var three = new[] {"One", "Three"};
List<string[]> list = new List<string[]>(){one, two, three};
var i = list.Select(l => new {Key = String.Join("|", l), Values = l})
.GroupBy(l => l.Key)
.Select(l => l.First().Values)
.ToArray();
You might have to use ToList() after Distinct():
List<string[]> distinct = data.Distinct().ToList();

SQL Like search in LIST and LINQ in C#

I have a List which contains the list of words that needs to be excluded.
My approach is to have a List which contains these words and use Linq to search.
List<string> lstExcludeLibs = new List<string>() { "CONFIG", "BOARDSUPPORTPACKAGE", "COMMONINTERFACE", ".H", };
string strSearch = "BoardSupportPackageSamsung";
bool has = lstExcludeLibs.Any(cus => lstExcludeLibs.Contains(strSearch.ToUpper()));
I want to find out part of the string strSearch is present in the lstExcludedLibs.
It turns out that .any looks only for exact match. Is there any possibilities of using like or wildcard search
Is this possible in linq?
I could have achieved it using a foreach and contains but I wanted to use LINQ to make it simpler.
Edit: I tried List.Contains but it also doesn't seem to work
You've got it the wrong way round, it should be:-
List<string> lstExcludeLibs = new List<string>() { "CONFIG", "BOARDSUPPORTPACKAGE", "COMMONINTERFACE", ".H", };
string strSearch = "BoardSupportPackageSamsung";
bool has = lstExcludeLibs.Any(cus => strSearch.ToUpper().Contains(cus));
Btw - this is just an observation but, IMHO, your variable name prefixes 'lst' and 'str' should be ommitted. This is a mis-interpretation of Hungarian notation and is redundant.
I think the line should be:
bool has = lstExcludeLibs.Any(cus => cus.Contains(strSearch.ToUpper()));
Is this useful to you ?
bool has = lstExcludeLibs.Any(cus => strSearch.ToUpper().Contains(cus));
OR
bool has = lstExcludeLibs.Where(cus => strSearch.ToUpper().IndexOf(cus) > -1).Count() > 0;
OR
bool has = lstExcludeLibs.Count(cus => strSearch.ToUpper().IndexOf(cus) > -1) > 0;

Categories

Resources