I have a "sort" query parameter. This maybe looking like this:
?sort=-name,+number
Now I want to read this and build a sort query for the c# MongoDB-Driver.
I thought of something like this:
var sortByList = parameters.Sort?.Split(',');
foreach (var sortBy in sortByList)
{
if (sortBy.StartsWith('-'))
// add to sort query decending ?!?
else
// add to sort query ascending ?!?
}
The only way I found so far is to build a string like "{name:-1, number:1}" and give this to the .Sort-method. But so I have build to build many strings that I really don't need.
Isn't there a way to do something like:
var sortDefinition = new SortDefintion();
var sortByList = parameters.Sort?.Split(',');
foreach (var sortBy in sortByList)
{
if (sortBy.StartsWith('-'))
sortDefinition.Add(sortBy.Substring(1), -1);
else
sortDefinition.Add(sortBy.Substring(1), 1);
}
return db.GetCollection<BsonDocument>("sortTest").Sort(sortDefintion).ToList();
You can solve it like this:
var bldr = Builders<BsonDocument>.Sort;
var sortDefinitions = sortByList.Select(x =>
{
SortDefinition<BsonDocument> sortDef;
var propName = x.Substring(1);
if (x.StartsWith("-"))
sortDef = bldr.Descending(propName);
else
sortDef = bldr.Ascending(propName);
return sortDef;
});
var sortDef = bldr.Combine(sortDefinitions);
return db
.GetCollection<BsonDocument>("sortTest")
.Find(Builders<BsonDocument>.Filter.Empty)
.Sort(sortDef)
.ToList();
Above code uses a Linq Select to create a SortDefinition<BsonDocument> for each string in the list and combines these definitions into a single sort definition afterwards. This sort definition is then applied to the Find.
Related
I have an object with a String field containing a comma separated list of integers in it. I'm trying to use LINQ to retrieve the ones that have a specific number in the list.
Here's my approach
from p in source
where (p.Keywords.Split(',').something.Contains(val))
select p;
Where p.Keywords is the field to split.
I've seen the following in the net but just doesn't compile:
from p in source
where (p.Keywords.Split(',').Select(x=>x.Trim()).Contains(val))
select p;
I'm a LINQ newbie, but had success with simpler queries.
Update:
Looks like I was missing some details:
source is a List containing the object with the field Keywords with strings like 1,2,4,7
Error I get is about x not being defined.
Here's an example of selecting numbers that are greater than 3:
string str = "1,2,3,4,5,6,7,8";
var numbers = str.Split(',').Select(int.Parse).Where(num => num > 3); // 4,5,6,7,8
If you have a list then change the Where clause:
string str = "1,2,3,4,5,6,7,8";
List<int> relevantNums = new List<int>{5,6,7};
var numbers = str.Split(',').Select(int.Parse).Where(num => relevantNums.Contains(num)); // 5,6,7
If you are not looking for number but for strings then:
string str = "1,2,3,4,5,6,7,8";
List<string> relevantNumsStr = new List<string>{"5","6","7"};
var numbers = str.Split(',').Where(numStr => relevantNumsStr.Contains(numStr)); // 5,6,7
Here is an example of how you can achieve this. For simplicity I did to string on the number to check for, but you get the point.
// class to mimic what you structure
public class MyObj
{
public string MyStr{get;set;}
}
//method
void Method()
{
var myObj = new List <MyObj>
{
new MyObj{ MyStr="1,2,3,4,5"},
new MyObj{ MyStr="9,2,3,4,5"}
};
var num =9;
var searchResults = from obj in myObj
where !string.IsNullOrEmpty(obj.MyStr) &&
obj.MyStr.Split(new []{','})
.Contains(num.ToString())
select obj;
foreach(var item in searchResults)
Console.WriteLine(item.MyStr);
}
Thanks for all the answers, although not in the right language they led me to the answer:
from p in source where (p.Keywords.Split(',').Contains(val.ToString())) select p;
Where val is the number I'm looking for.
I'm trying to assemble ad-hoc queries to Breeze.
I have a physician.contact.addresses relationship.
When I try:
myPred = new pred('contact.addresses.street1', op.StartsWith, "a");
And execute it I get:
"The parent value for a property access of a property 'Addresses' is not a single value. Property access can only be applied to a single value."
To try a work-around I've tried parsing out those many-relationships and am passing it to the breeze controller in .withParameters like this:
var criteriaStr = toManyArray.length ? ko.utils.stringifyJson(toManyArray) : "";
query = query.withParameters({ searchParms: criteriaStr });
where toManyArray is an array of fieldName:value pairs.
on the controller side:
[HttpGet]
public IQueryable<Physician> Physician(string searchParms = null)
{
if (searchParms != null)
{
var ser = new JavaScriptSerializer();
var searchCritAry = ser.Deserialize<String[]>(searchParms);
foreach (var aryItem in searchCritAry)
{
// aryItem looks like this:
// f_str_street_from_addresses:a
var predEnt = aryItem.Split(':')[0].Split('_')[4];
var predField = aryItem.Split(':')[0].Split('_')[2];
var predVal = aryItem.Split(':')[1];
switch (predEnt)
{
case "addresses":
switch (predField)
{
case "street":
//physPool =
_contextProvider.Context.Physicians
.Where(p => p.Contact.Addresses.Any(a => a.Street1.StartsWith(predVal)));
break;
case "street2":
//physPool =
_contextProvider.Context.Physicians
.Where(p => p.Contact.Addresses.Any(a => a.Street2.StartsWith(predVal)));
break;
}
break;
}
}
// here I want to combine the .Where clauses from above with the breeze preds
return _contextProvider.Context.Physicians;
}
return _contextProvider.Context.Physicians;
}
It's not working and only returning a selection using the predicates that are passed in the normal way through Breeze's query. I don't see how to pass the filtered IQueryable to Breeze's _contextProvider.
Thanks for any suggestions.
Take a look at the new "any/all" support in Breeze ( as of version 1.4.7). Some examples may be found here: http://www.breezejs.com/documentation/query-examples
You can construct your query/predicate like this:
var pred = new Breeze.Predicate('contact.addresses', "any", 'street1', op.StartsWith, "a");
var query = EntityQuery.from("Physicians").where(pred);
or simply
var query = EntityQuery.from("Physicians")
.where("contact.addresses", "any", "street1", "startsWith", 'a')
You may want to add an expand if you want the contact and address information sent down as well.
query = query.expand("contact.addresses");
If you have a need for nested any/all expressions you may need to add the following configuration to your BreezeController to determine just how deep you want to allow the expressions to go (2 in the example below):
[BreezeController(MaxAnyAllExpressionDepth = 2)]
I have a list of strings:
var list = new List<string>();
list.Add("CAT");
list.Add("DOG");
var listofItems = new List<string>();
listofItems .Add("CATS ARE GOOD");
listofItems .Add("DOGS ARE NICE");
listofItems .Add("BIRD");
listofItems .Add("CATAPULT");
listofItems .Add("DOGGY");
and now i want a function like this:
listofItems.Where(r=> list.Contains(r));
but instead of Contains, i want it to do a starts with check so 4 out of the 5 items would be returned (BIRD would NOT).
What is the fastest way to do that?
You can use StartsWith inside of an Any
listofItems.Where(item=>list.Any(startsWithWord=>item.StartsWith(startsWithWord)))
You can visualize this as a double for loop, with the second for breaking out as soon as it hits a true case
var filteredList = new List<String>();
foreach(var item in listOfItems)
{
foreach(var startsWithWord in list)
{
if(item.StartsWith(startsWithWord))
{
filteredList.Add(item)
break;
}
}
}
return filteredList;
The fastest way would be usage of another data structure, for example Trie. Basic C# implementation can be found here: https://github.com/kpol/trie
This should get you what you need in a more simplified format:
var result = listofItems.Select(n =>
{
bool res = list.Any(v => n.StartsWith(v));
return res
? n
: string.Empty;
}).Where(b => !b.Equals(string.Empty));
The Trie data structure is what you need. Take a look at this more mature library: TrieNet
using Gma.DataStructures.StringSearch;
...
var trie = new SuffixTrie<int>(3);
trie.Add("hello", 1);
trie.Add("world", 2);
trie.Add("hell", 3);
var result = trie.Retrieve("hel");
I have a web page in which I am giving USER the options of writing notes. Now when ever the web page checks that a USER is:abc then it pulls up the note from the MEMO Table.
Here is my code in Page_Load():
using (EntityMemoDataContext em = new EntityMemoDataContext())
{
int getEntity = Int16.Parse(Session["EntityIdSelected"].ToString());
var showMemo = from r in em.EntityMemoVs_1s
where r.EntityID == getEntity
select r.Memo;
tbShowNote.Text = String.Join(#"<br />", showMemo);
}
tbShowNote is showing me value like this:
test<br />test1<br />test1<br />test4<br />test4
And I want it like this:
Test
Test1
Test2 ...
tbShowNote is a TextBox!
You only asked for the first memo, so that's what you got back. If you want it enumerated with each one on it's own line in html, you could do this:
using (EntityMemoDataContext em = new EntityMemoDataContext())
{
int getEntity1 = Int16.Parse(Session["EntityIdSelected"].ToString());
var showMemo = from r in em.EntityMemoVs_1s
where r.EntityID == getEntity1
select new
{
r.Memo
};
tbShowNote.Text = String.Join(#"<br />", showMemo);
}
The key takeaway is if r.Memo is of type string, then the LINQ query you executed gave you back a IQueryable<string>. It's on you to decide if you want to flatten that list later.
Edit: Equiso made a good observation in that you're actually returning an IQueryable of an anonymous type, not IQueryable<string> due to the new { ... } syntax. I'd say combine his answer with mine and run with it:
var showMemo = from r in em.EntityMemoVs_1s
where r.EntityID == getEntity1
select r.Memo;
tbShowNote.Text = String.Join(#"<br />", showMemo);
The problem is in the select part of your linq query, you are wrapping your results in an anonymous type, that is why when you call ToString() you see { Memo = test }. You probably want it like this:
var showMemo = from r in em.EntityMemoVs_1s
where r.EntityID == getEntity1
select r.Memo;
After that showMemo will contain just strings.
It looks like your showMemo is a collection and you are then just assigning the top value? If you are putting them in one string then you need to aggregate them together.
I have a requirement to extract a distinct subset of rows from a DataTable, and thought LINQ2DataSets may be a useful and clean way to do this, however it appears that it is not possible to simply identify return rows from a LINQ2DS query as follows
var result = from r in fips.AsEnumerable() select
r.Field<string>("FACILITY_PROCESS_SUB_GROUP_CODE"),
r.Field<string>("PROCESS_SUB_GROUP_NAME"),
r.Field<string>("...
as I start getting errors after the first comma.
Is this a correct assumption, and how would I get around it to return a subset of columns from the dataset that I can apply a Distinct() method to?
You forgot the new statement and field names:
var result = from r
in fips.AsEnumerable()
select new
{
FacProcess = r.Field<string>("FACILITY_PROCESS_SUB_GROUP_CODE"),
GroupName = r.Field<string>("PROCESS_SUB_GROUP_NAME"),
Item3 = r.Field<string>("Item3")
};
You can also explicitly declare that you are going to use a type:
var result = from r
in fips.AsEnumerable()
select new MyType("InitClassParams")
{
FacProcess = r.Field<string>("FACILITY_PROCESS_SUB_GROUP_CODE"),
GroupName = r.Field<string>("PROCESS_SUB_GROUP_NAME"),
Item3 = r.Field<string>("Item3")
};
Scott Guthrie (VP Developer Devision, Microsoft) has some good info about LINQ (he talks about LINQ to SQL, but most of it applies regardless).
Then apply the distinct clause:
var result = from r
in fips.AsEnumerable()
select new
{
FacProcess = r.Field<string>("FACILITY_PROCESS_SUB_GROUP_CODE"),
GroupName = r.Field<string>("PROCESS_SUB_GROUP_NAME"),
Item3 = r.Field<string>("Item3")
}
distinct;
Then put it to a list or iterate over it. Nothing will be selected/distincted/etc until something like on of the following is run:
var list = result.ToList()
foreach(var item in result) {}