Hi I am trying to find the position of specific element from IQueryable. I dont want to covert to list then search for it because the process is heavy if the number of element is huge.
I tried .TakeWhile(x => x.ItemId.Equals(ItemId)) but it show me function not supported.
Below is how I doing it now using loop through the query. Is there any better approach for it?
IQueryable<CustomSearchResultItem> contextQueryable = context.GetQueryable<CustomSearchResultItem>().Where(query);
if (ItemId != ID.Null)
{
int i = 0;
foreach (var x in contextQueryable)
{
if(x.ItemId.Equals(ItemId))
{
Position = i;
break;
}
i++;
}
}
Something like this should work:
var contextQueryable = context.GetQueryable<CustomSearchResultItem>().Where(query).GetResults();
var result = contextQueryable.Select((x, i) => new { Item = x, Index = i })
.FirstOrDefault(itemWithIndex => itemWithIndex.Item.Document.ItemId.Guid == ItemId);
if (result != null)
index = result.Index;
Related
I have a simple class Item:
public class Item
{
public int Start { get; set;}
public int Stop { get; set;}
}
Given a List<Item> I want to split this into multiple sublists of contiguous elements. e.g. a method
List<Item[]> GetContiguousSequences(Item[] items)
Each element of the returned list should be an array of Item such that list[i].Stop == list[i+1].Start for each element
e.g.
{[1,10], [10,11], [11,20], [25,30], [31,40], [40,45], [45,100]}
=>
{{[1,10], [10,11], [11,20]}, {[25,30]}, {[31,40],[40,45],[45,100]}}
Here is a simple (and not guaranteed bug-free) implementation that simply walks the input data looking for discontinuities:
List<Item[]> GetContiguousSequences(Item []items)
{
var ret = new List<Item[]>();
var i1 = 0;
for(var i2=1;i2<items.Length;++i2)
{
//discontinuity
if(items[i2-1].Stop != items[i2].Start)
{
var num = i2 - i1;
ret.Add(items.Skip(i1).Take(num).ToArray());
i1 = i2;
}
}
//end of array
ret.Add(items.Skip(i1).Take(items.Length-i1).ToArray());
return ret;
}
It's not the most intuitive implementation and I wonder if there is a way to have a neater LINQ-based approach. I was looking at Take and TakeWhile thinking to find the indices where discontinuities occur but couldn't see an easy way to do this.
Is there a simple way to use IEnumerable LINQ algorithms to do this in a more descriptive (not necessarily performant) way?
I set of a simple test-case here: https://dotnetfiddle.net/wrIa2J
I'm really not sure this is much better than your original, but for the purpose of another solution the general process is
Use Select to project a list working out a grouping
Use GroupBy to group by the above
Use Select again to project the grouped items to an array of Item
Use ToList to project the result to a list
public static List<Item[]> GetContiguousSequences2(Item []items)
{
var currIdx = 1;
return items.Select( (item,index) => new {
item = item,
index = index == 0 || items[index-1].Stop == item.Start ? currIdx : ++currIdx
})
.GroupBy(x => x.index, x => x.item)
.Select(x => x.ToArray())
.ToList();
}
Live example: https://dotnetfiddle.net/mBfHru
Another way is to do an aggregation using Aggregate. This means maintaining a final Result list and a Curr list where you can aggregate your sequences, adding them to the Result list as you find discontinuities. This method looks a little closer to your original
public static List<Item[]> GetContiguousSequences3(Item []items)
{
var res = items.Aggregate(new {Result = new List<Item[]>(), Curr = new List<Item>()}, (agg, item) => {
if(!agg.Curr.Any() || agg.Curr.Last().Stop == item.Start) {
agg.Curr.Add(item);
} else {
agg.Result.Add(agg.Curr.ToArray());
agg.Curr.Clear();
agg.Curr.Add(item);
}
return agg;
});
res.Result.Add(res.Curr.ToArray()); // Remember to add the last group
return res.Result;
}
Live example: https://dotnetfiddle.net/HL0VyJ
You can implement ContiguousSplit as a corutine: let's loop over source and either add item into current range or return it and start a new one.
private static IEnumerable<Item[]> ContiguousSplit(IEnumerable<Item> source) {
List<Item> current = new List<Item>();
foreach (var item in source) {
if (current.Count > 0 && current[current.Count - 1].Stop != item.Start) {
yield return current.ToArray();
current.Clear();
}
current.Add(item);
}
if (current.Count > 0)
yield return current.ToArray();
}
then if you want materialization
List<Item[]> GetContiguousSequences(Item []items) => ContiguousSplit(items).ToList();
Your solution is okay. I don't think that LINQ adds any simplification or clarity in this situation. Here is a fast solution that I find intuitive:
static List<Item[]> GetContiguousSequences(Item[] items)
{
var result = new List<Item[]>();
int start = 0;
while (start < items.Length) {
int end = start + 1;
while (end < items.Length && items[end].Start == items[end - 1].Stop) {
end++;
}
int len = end - start;
var a = new Item[len];
Array.Copy(items, start, a, 0, len);
result.Add(a);
start = end;
}
return result;
}
I have a repeating foreach condition in my controller. How can I simplify it?
I almost reach 500 lines because of this. I've been using this for x8 each condition.
List<jewelry_dashboard_view_per_month> transactionmonthlynewloan = dashboardmanager.Get_MonthlyTransaction(search_branch, (monthlyonly + "01"), "N-", (monthlyonly + no_of_items), no_of_items, monthlyonly);
myNewLoanMontlyList.Add(transactionmonthlynewloan);
List<jewelry_dashboard_view_per_month> transactionmonthlyrenewal = dashboardmanager.Get_MonthlyTransaction(search_branch, (monthlyonly + "01"), "R-", (monthlyonly + no_of_items), no_of_items, monthlyonly);
myRenewalMontlyList.Add(transactionmonthlyrenewal);
This is the if condition
if (myNewLoanMontlyList[0].Count != 0)
{
foreach (var internal_monthly_newloan_data in myNewLoanMontlyList[0].SelectMany(c => c.id_data))
{monthly_newloan_data_ID.Add(internal_monthly_newloan_data);}
foreach (var internal_monthly_newloan_data in myNewLoanMontlyList[0].SelectMany(c => c.debit_data))
{monthly_newloan_data_debit.Add(internal_monthly_newloan_data);}
}
else
{
monthly_newloan_data_ID.Add(0);
monthly_newloan_data_debit.Add(0);
};
and this is the foreach condition
//newloan
int newloan_data_id = 0;
DateTime newloan_data_transdate = DateTime.Parse((DateTime.Today).ToString());
decimal newloan_data_debit = 0;
string newloan_data_txnname = "";
string newloan_data_branchID = "";
foreach (var newloan_data in newloan)
{
newloan_data_id = newloan_data.ID;
newloan_data_transdate = DateTime.Parse((newloan_data.Transdate).ToString());
newloan_data_debit = Decimal.Parse((newloan_data.Debit).ToString());
newloan_data_txnname = newloan_data.TransactionName;
newloan_data_branchID = newloan_data.BranchID;
};
datanewloan = new transaction_details()
{
ID = newloan_data_id,
Transdate = DateTime.Parse(newloan_data_transdate.ToString("yyyy-MM-dd")),
Debit = Decimal.Parse(newloan_data_debit.ToString()),
TransactionName = newloan_data_txnname,
BranchID = newloan_data_branchID
};
In Your scenario of if-else conditions:
if (myNewLoanMontlyList[0].Count != 0)
{
foreach (var internal_monthly_newloan_data in myNewLoanMontlyList[0].SelectMany(c => c.id_data))
{monthly_newloan_data_ID.Add(internal_monthly_newloan_data);}
foreach (var internal_monthly_newloan_data in myNewLoanMontlyList[0].SelectMany(c => c.debit_data))
{monthly_newloan_data_debit.Add(internal_monthly_newloan_data);}
}
else
{
monthly_newloan_data_ID.Add(0);
monthly_newloan_data_debit.Add(0);
};
If in the method, there is no other process after the if-else, you can use only if condition without the else part.
if (myNewLoanMontlyList[0].Count == 0)
{
monthly_newloan_data_ID.Add(0);
monthly_newloan_data_debit.Add(0);
}
foreach (var internal_monthly_newloan_data in myNewLoanMontlyList[0].SelectMany(c => c.id_data))
{monthly_newloan_data_ID.Add(internal_monthly_newloan_data);}
foreach (var internal_monthly_newloan_data in myNewLoanMontlyList[0].SelectMany(c => c.debit_data))
{monthly_newloan_data_debit.Add(internal_monthly_newloan_data);}
You can use Linq,
SelectMany: Projects each element of a sequence to an IEnumerable<T>. You need not to iterate again and add it to separate list
For your if condition look like,
if (myNewLoanMontlyList[0].Any())
{
monthly_newloan_data_ID = myNewLoanMontlyList[0].SelectMany(c => c.id_data);
monthly_newloan_data_debit = myNewLoanMontlyList[0].SelectMany(c => c.debit_data);
}
else
{
monthly_newloan_data_ID.Add(0);
monthly_newloan_data_debit.Add(0);
}
Select: Projects each element of a sequence into a new form. In your
case new form is instance of transaction_details
Instead of for loop use Linq .Select(),
var result = newloan.Select(x => new transaction_details(){
ID = x.ID,
Transdate = DateTime.Parse(x.Transdate.ToString("yyyy-MM-dd")),
Debit = Decimal.Parse((x.Debit).ToString()),
TransactionName = x.TransactionName,
BranchID = x.BranchID
}).LastOrDefault();
To get last element, I used LastOrDefault(). You can get individual element by index or by condition.
I am working with two lists. The first contains a large sequence of strings. The second contains a smaller list of strings. I need to find where the second list exists in the first list.
I worked with enumeration, and due to the large size of the data, this is very slow, I was hoping for a faster way.
List<string> first = new List<string>() { "AAA","BBB","CCC","DDD","EEE","FFF" };
List<string> second = new List<string>() { "CCC","DDD","EEE" };
int x = SomeMagic(first,second);
And I would need x to = 2.
Ok, here is my variant with old-good-for-each-loop:
private int SomeMagic(IEnumerable<string> source, IEnumerable<string> target)
{
/* Some obvious checks for `source` and `target` lenght / nullity are ommited */
// searched pattern
var pattern = target.ToArray();
// candidates in form `candidate index` -> `checked length`
var candidates = new Dictionary<int, int>();
// iteration index
var index = 0;
// so, lets the magic begin
foreach (var value in source)
{
// check candidates
foreach (var candidate in candidates.Keys.ToArray()) // <- we are going to change this collection
{
var checkedLength = candidates[candidate];
if (value == pattern[checkedLength]) // <- here `checkedLength` is used in sense `nextPositionToCheck`
{
// candidate has match next value
checkedLength += 1;
// check if we are done here
if (checkedLength == pattern.Length) return candidate; // <- exit point
candidates[candidate] = checkedLength;
}
else
// candidate has failed
candidates.Remove(candidate);
}
// check for new candidate
if (value == pattern[0])
candidates.Add(index, 1);
index++;
}
// we did everything we could
return -1;
}
We use dictionary of candidates to handle situations like:
var first = new List<string> { "AAA","BBB","CCC","CCC","CCC","CCC","EEE","FFF" };
var second = new List<string> { "CCC","CCC","CCC","EEE" };
If you are willing to use MoreLinq then consider using Window:
var windows = first.Window(second.Count);
var result = windows
.Select((subset, index) => new { subset, index = (int?)index })
.Where(z => Enumerable.SequenceEqual(second, z.subset))
.Select(z => z.index)
.FirstOrDefault();
Console.WriteLine(result);
Console.ReadLine();
Window will allow you to look at 'slices' of the data in chunks (based on the length of your second list). Then SequenceEqual can be used to see if the slice is equal to second. If it is, the index can be returned. If it doesn't find a match, null will be returned.
Implemented SomeMagic method as below, this will return -1 if no match found, else it will return the index of start element in first list.
private int SomeMagic(List<string> first, List<string> second)
{
if (first.Count < second.Count)
{
return -1;
}
for (int i = 0; i <= first.Count - second.Count; i++)
{
List<string> partialFirst = first.GetRange(i, second.Count);
if (Enumerable.SequenceEqual(partialFirst, second))
return i;
}
return -1;
}
you can use intersect extension method using the namepace System.Linq
var CommonList = Listfirst.Intersect(Listsecond)
Lets say I have this code.
int count = 0;
int id = GetFirstId();
count = getCountById(id);
if(count == 0){
id = GetSecondId();
count = getCountById(id);
if(count == 0){
id = GetThirdId();
count = getCountById(id);
}
}
Is there a better way to do this. Is something like a LOOP and CASE Statement inside better?
If you have different method, you can list the methods and then use LINQ to get first non-zero count. It will work if all of your GetXXXId have same signature.
var idGetters = new Func<int>[]
{
GetFirstId,
GetSecondId,
GetThirdId
// and so on
};
var count = idGetters
.Select(x => x())
.Select(GetCountById)
.SkipWhile(x => x == 0)
.FirstOrDefault();
mI am comparing Listview items with Generic List items with List.Any Method like this:
foreach (ListViewItem itemRow in lstviewAddsheets.Items)
{
if (InvalidSheets.Any(x => x != null && x.FilePath == itemRow.Tag.ToString()))
{
//Math found
}
}
Please tell me, how to get InvalidSheets list index which was matched with itemRow.Tag.ToString().
Since there seems some debate about how much faster it would be to use List.FindIndex() instead of Linq to find the index, I wrote a test program.
This assumes that you only care about finding the index of the first matching item in a list. It doesn't handle multiple matching items.
Also note that this test is worst-case in that the matching item is at the very end of the list.
My results for an x86 release build (run on Windows 8 x64, quad core processor):
Calling Via FindIndex() 100 times took 00:00:00.9326057
Calling Via Linq 100 times took 00:00:04.0014677
Calling Via FindIndex() 100 times took 00:00:00.8994282
Calling Via Linq 100 times took 00:00:03.9179414
Calling Via FindIndex() 100 times took 00:00:00.8971618
Calling Via Linq 100 times took 00:00:03.9134804
Calling Via FindIndex() 100 times took 00:00:00.8963758
showing that List.FindIndex() is roughly four times faster than using Linq.
Here's the test code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Demo
{
class Test
{
public string FilePath;
}
class Program
{
private void run()
{
int count = 1000000;
List<Test> list = new List<Test>(count);
for (int i = 0; i < count; ++i)
list.Add(new Test{ FilePath = i.ToString()});
string target = (count-1).ToString();
for (int trial = 0; trial < 4; ++trial)
{
Action viaFindIndex =
(
() =>
{
int index = list.FindIndex(x => (x != null) && (x.FilePath == target));
}
);
Action viaLinq =
(
() =>
{
int index = list.Select((x, i) => new { Item = x, Index = i })
.First(x => (x != null) && (x.Item.FilePath == target))
.Index;
}
);
viaFindIndex.TimeThis("Via FindIndex()", 100);
viaLinq.TimeThis("Via Linq", 100);
}
}
private static void Main()
{
new Program().run();
}
}
static class DemoUtil
{
public static void TimeThis(this Action action, string title, int count = 1)
{
var sw = Stopwatch.StartNew();
for (int i = 0; i < count; ++i)
action();
Console.WriteLine("Calling {0} {1} times took {2}", title, count, sw.Elapsed);
}
}
}
So given that List.FindIndex() is both much faster AND much easier to read than using the Linq, I can see no reason to use Linq to solve this particular problem.
int index = list.FindIndex(x => (x != null) && (x.FilePath == target));
versus
int index = list.Select((x, i) => new { Item = x, Index = i })
.First(x => (x != null) && (x.Item.FilePath == target))
.Index;
The first version wins on all counts IMO.
You can do this
int index = InvalidSheets.FindIndex(x => x != null && x.FilePath == itemRow.Tag.ToString());
if you want to get the object directly then do this
var matchedObject = InvalidSheets.FirstOrDefault(x => x != null && x.FilePath == itemRow.Tag.ToString());
Here is how you can get the index:
var index = InvalidSheets.Select((x, i) => new {Item = x, Index = i})
.First(x => x.Item != null && x.Item.FilePath == itemRow.Tag.ToString())
.Index;
However you might want to refactor this with FirstOrDefault like this:
foreach (ListViewItem itemRow in lstviewAddsheets.Items)
{
var sheet = InvalidSheets.Select((x, i) => new {Item = x, Index = i})
.FirstOrDefault(x => x.Item != null && x.Item.FilePath == itemRow.Tag.ToString());
if (sheet != null)
{
var index = sheet.Index;
}
}
Try this:
InvalidSheets.IndexOf(InvalidSheets.First(x => x != null && x.FilePath == itemRow.Tag.ToString()))
It will get index of first invalid sheet matching the predicate
You can project the index with the overload, therefore you need to select an anonymous type:
var invalids = InvalidSheets.Select((s, i) => { Sheet=s, Index=i })
.Where(x => x.Sheet != null && x.Sheet.FilePath == itemRow.Tag.ToString()));
bool anyInvalid = invalids.Any(); // is any invalid
IEnumerable<int> indices = invalids.Select(x => x.Index);// if you need all indices