The query results cannot be enumerated more than once - c#

Consider the following methods. I am getting exception as asked , while repeater binding.
Bindrepeater:
private void BindRepeater()
{
var idx = ListingPager.CurrentIndex;
int itemCount;
var keyword = Keywords.Text.Trim();
var location = Area.Text.Trim();
var list = _listing.GetBusinessListings(location, keyword, idx, out itemCount);
ListingPager.ItemCount = itemCount;
BusinessListingsRepeater.DataSource = list.ToList(); // exception here
BusinessListingsRepeater.DataBind();
}
GetBusinessListings:
public IEnumerable<Listing> GetBusinessListings(string location, string keyword, int index, out int itemcount)
{
var skip = GetItemsToSkip(index);
var result = CompiledQueries.GetActiveListings(Context);
if (!string.IsNullOrEmpty(location))
{
result= result.Where(c => c.Address.Contains(location));
}
if (!string.IsNullOrEmpty(keyword))
{
result = result.Where(c => c.RelatedKeywords.Contains(keyword) || c.Description.Contains(keyword));
}
var list = result;
itemcount = list.Count();
return result.Skip(skip).Take(10);
}
GetActiveListings :
/// <summary>
/// Returns user specific listing
/// </summary>
public static readonly Func<DataContext, IQueryable<Listing>> GetActiveListings =
CompiledQuery.Compile((DataContext db)
=> from l in db.GetTable<Listing>()
where l.IsActive
select l);

When you assign itemcount you are executing the query the first time. Why do you need this?
My suggestion would be not to retrieve the item count there and just
return result.Skip(skip).Take(10).ToList();
Basically you can't have both, fetch only the results you need and retrieve the total count in one query. You could use two seperate querys though.

You may want to persist your results as a collection, not an IQueryable.
var list = result.ToArray();
itemcount = list.Length;
return list.Skip(skip).Take(10);
The above code may not be correct for paging. You likely will have to run the query twice.

Related

How convert IQueryable to list for showing return data?

I have a variable which is IQueryable and I have parameter in my viewmodel that it shows a differences between my selected day and today the code is:
public async Task<ResultData<HomePlannedRecieptInfo>> GetHomePlannedReciepts(HomePlannedRecieptQuery query)
{
var datelist = new List<DateTime>();
var date = DateTime.Now;
for (var i = 0; i <= 6; i++)
{
datelist.Add(date.AddDays(i));
}
datelist.Add(date);
IQueryable<HomePlannedRecieptInfo> result = context.HomePlannedRecieptInfos.Where(t=>t.Date >= DateTime.Today);
foreach (var item in result.ToList())
{
item.DayDiff = (item.Date.Date - DateTime.Now.Date).TotalDays.ToString();
}
var offset = (query.PageNumber - 1) * query.PageSize;
var psize = query.PageSize;
var countQuery = await result.CountAsync();
var data = result.OrderByDescending(c => c.Date);
return new ResultData<HomePlannedRecieptInfo>
{
CurrentPage = query.PageNumber,
TotalItems = countQuery,
PageSize = query.PageSize,
Data = data.ToList()
};
when I execute the code in foreach I can see the number of days in item.dayDiff but I can't see it when the the data return. Can somebody help me please?
You're calling result.ToList(), but you're just iterating over it in the foreach loop, you're not actually storing it anywhere. This should fix it:
var resultAsList = result.ToList();
foreach (var item in resultAsList)
{
item.DayDiff = (item.Date.Date - DateTime.Now.Date).TotalDays.ToString();
}
...
var data = resultAsList.OrderByDescending(c => c.Date);
That is because every time you materialize a IQueryable the data is taken from the database. You materializing twice in your code. Once in the foreach and once when returning the data in data.ToList(). Hence you have two lists with different items. Since you are changing the data in the first list the second will not receive that data.
You have two possibilities:
Call ToList before the foreach and use the same materialized list for changing the property and returning the same list
Change the get acessor for DayDiff to following public string DayDiff {get => (Date.Date - DateTime.Now.Date).TotalDays.ToString();} Using this has the advantage that you do not have to remind you to set the DayDiff if you query the data on other locations of your code

Split a list of objects into sub-lists of contiguous elements using LINQ?

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;
}

IEnumerable failed to set element

I have a ViewModel that contains different elements inside different tables that I tend to assign to it by query.
My problem is that I can't do this with IEnumerable (in GetAll() below), it keeps returning me null for RoomCode but for a single item (in GetDeviceId() below) then it works fine.
public IEnumerable<DeviceViewModel> GetAll()
{
var result = deviceRepository.GetAll().Select(x => x.ToViewModel<DeviceViewModel>());
for(int i = 0; i < result.Count(); i++)
{
int? deviceID = result.ElementAt(i).DeviceId;
result.ElementAt(i).RoomCode = deviceRepository.GetRoomCode(deviceID);
}
return result;
}
public DeviceViewModel GetDeviceID(int deviceID)
{
var result = new DeviceViewModel();
var device = deviceRepository.Find(deviceID);
if (device != null)
{
result = device.ToViewModel<DeviceViewModel>();
result.RoomCode = deviceRepository.GetRoomCode(deviceID);
}
else
{
throw new BaseException(ErrorMessages.DEVICE_LIST_EMPTY);
}
return result;
}
public string GetRoomCode(int? deviceID)
{
string roomCode;
var roomDevice = dbContext.Set<RoomDevice>().FirstOrDefault(x => x.DeviceId == deviceID && x.IsActive == true);
if (roomDevice != null)
{
var room = dbContext.Set<Room>().Find(roomDevice.RoomId);
roomCode = room.RoomCode;
}
else
{
roomCode = "";
}
return roomCode;
}
First, you need to materialize the query to a collection in local memory. Otherwise, the ElementAt(i) will query the db and give back some kind of temporary object each time it is used, discarding any change you do.
var result = deviceRepository.GetAll()
.Select(x => x.ToViewModel<DeviceViewModel>())
.ToList(); // this will materialize the query to a list in memory
// Now modifications of elements in the result IEnumerable will be persisted.
You can then go on with the rest of the code.
Second (and probably optional), I also recommend for clarity to use foreach to enumerate the elements. That's the C# idiomatic way to loop through an IEnumerable:
foreach (var element in result)
{
int? deviceID = element.DeviceId;
element.RoomCode = deviceRepository.GetRoomCode(deviceID);
}

Querying the id of an entity using linq

In the following method I am trying to fetch the Username by passing the id value where the ids passed as parameter can be multiple values as in csv's (eg: 1,2) and are returned to the calling function as IEnumerable.
Code Follows as below :
[NonAction]
public static IEnumerable<UserProfile> SearchCMSAdmins(string s)
{
//var searchResults = Entities.UserProfiles.Where(item =>item.UserName.Contains(s));
//return searchResults;
string[] ids = s.Split(',');
IEnumerable<UserProfile> results = null;
IList<UserProfile> user = new List<UserProfile>();
for (int i = 0; i < ids.Length; i++)
{
int id = Convert.ToInt32(ids[i].ToString());
var entity = Entities.UserProfiles.Where(item => item.UserId);
//user.Add(entity);
results = results.Concat(entity);
}
return results;
}
Any help is appreciated.
Try using Contains:
var results = Entities.UserProfiles.Where(item => ids.Contains(item.UserId));
http://msdn.microsoft.com/en-us/library/ms132407.aspx
You can get the id array to be of int type, You can either use int.TryParse or Convert.ToInt32 like:
int[] ids = s.Split(',').Select(r=> Convert.ToInt32(r)).ToArray();
Later you can modify your LINQ query as:
IList<UserProfile> user = Entities.UserProfiles
.Where(item=> ids.Contains(item)).ToList();
This would be like Select * from table where ID in (1,2,3) see Creating IN Queries With Linq to SQL for idea
[NonAction]
public static IEnumerable<UserProfile> SearchCMSAdmins(string s)
{
string[] ids = s.Split(',');
foreach (string idAsString in ids)
{
int id = Convert.ToInt32(idAsString);
var entity = Entities.UserProfiles.Where(item => item.UserId == id);
yield return entity;
}
}
should do it (there should be some validation code too in case the id is not an int or the entity is null)

how to sort mapreduce results?

I have written a method in C# which retrieves tweets from mongoDB and would like to count and sort authors by the number of retweets.
Right now, the method already performs map and reduce and returns unsorted results the following way:
public void RetweetsCount()
{
string wordMap = #"function wordMap() {
var usernameOrigin = this.text.match(/\brt\s*#(\w+)/i);
if (usernameOrigin === null) {
return;
}
// loop every word in the document
emit(usernameOrigin[1], { count : 1 });
}";
string wordReduce = #"function wordReduce(key, values) {
var total = 0;
for (var i = 0; i < values.length; i++) {
total += values[i].count;
}
return { count : total };
}";
var options = new MapReduceOptionsBuilder();
options.SetOutput(MapReduceOutput.Inline);
var results = collection.MapReduce(wordMap, wordReduce, options);
foreach (var result in results.GetResults())
{
Console.WriteLine(result.ToJson());
}
}
Does anyone know how sort results by descending count value (number of retweets)?
Here's the solution. After retrieving results from MapReduce, I first converted the IEnumerable to list and then ordered the list the folliwing way:
var results = collection.MapReduce(wordMap, wordReduce, options);
IEnumerable<BsonDocument> resultList = results.GetResults();
List<BsonDocument> orderedList = resultList.ToList().OrderByDescending(x => x[1]).ToList();

Categories