Background: I am using Linq to get the data. Wrote below query to get List<List<InboxProcessingDto>> , now I want to remove record who don't have consecutive sequenceId in each List<InboxProcessingDto>.
List<InboxProcessingDto> will be always sorted.
Linq Query:
_context.Inboxes
.Join(_context.InboxProcessingOffsets, x => x.SequenceSource, y => y.Source, (x, y) => new InboxProcessingDto { Inbox = x, InboxProcessingOffset = y })
.Where(i => (i.Inbox.Status == InboxStatus.New ))
.Take(100)
.ToList()
.GroupBy(i => i.Inbox. SequenceSource)
.Select(grp => grp.OrderBy(i => i.Inbox.SequenceId))
.ToListAsync();
Above query gives me data like below screenshot:
List at [0] contains 4 objects of InboxProcessingDto and this dto has sequenceID, list[0] might have objects who don't have consecutive sequence id eg. 22, 23,27,28
from this list I want to remove 2 objects who has sequenceId 27,28
as we can see in screenshot next list from List<List> contains 5 objects of InboxProcessingDto and this dto has sequenceID, it might have objects who don't have consecutive sequence id eg. 2, 3,17,28,29
from this list I want to remove 3 objects who has sequenceId 17,28,29
I have a dataset with a column having Numeric and AlphaNumeric Order Nos. Data is being grouped up with different columns which result in merging rows together. Now I want to group lines based on Numeric and AlphaNumeric Order Nos as well, for that purpose I have to evaluate string (data) whether it is Numeric or AlphaNumeric. Following is the statement which groups up data together.
var groupLinesETAFile = (from g in strLinesETAFile
group g by new
{
g.PONo,
g.POLineNo,
g.POSubLineNo,
g.AllocatedPartNo
} into grouped
select new
{
PONo = grouped.Key.PONo,
POLineNo = grouped.Key.POLineNo,
POSubLineNo = grouped.Key.POSubLineNo,
AllocatedPartNo = grouped.Key.AllocatedPartNo,
ETAQty = grouped.Sum(x => decimal.Parse(x.ETAQty))
}).ToList();
Now I want to add a new column in statement "SupplierOrderNo" which has Numeric and AlphaNumeric Values and want to group on Numeric and AlphaNumeric values as well. How can I achieve this with LINQ?
I have a database table with a column name FILES.
The data for the FILES column looks like this
//directory/anotherdirectory/file1.txt
//directory/anotherdirectory/file2.txt
//directory/anotherdirectory/file3.txt
I want the linq to create a string like
"file1.txt\nfile2.txt\file3.txt"
I want to be able to select the column and parse the data at the same time.
The following two ways give errors.
string filesNames = string.Join("\n", _repositoryFactory
.GetRepository<MyResponseEntity>()
.Entity
.ByMyId(id).Select(x => {x.FILES = "Just a test"; return x}).ToArray());
I have also tried do a substring with no luck
string filesNames = string.Join("\n", _repositoryFactory
.GetRepository<MyResponseEntity>()
.Entity
.ByMyId(id).Select(x => x.FILES.Substring(x.FILES.LastIndexOf('/')+1,
x.FILES.Length - (x.FILES.LastIndexOf('/')+1)).ToArray());
Using Aggregate method.
var fileNames = _repositoryFactory
.GetRepository<MyResponseEntity>()
.Entity
.ByMyId(id)
.Select(e => e.Substring(e.LastIndexOf('/') + 1))
.Aggregate((a, b) => a + "\n" + b);
string filesNames = string.Join("\n", _repositoryFactory.GetRepository<MyResponseEntity>()
.Entity.ByMyId(id).Select(x => Path.GetFileName(x.FILES)).ToArray());
Basically I have a csv of the format of shown below:
The csv has 11 columns with the first five and last five being the exact same.
I want to be able to read in the csv and store all instances of the first and fifth column (Period and Payout) in a list where they have values and do the same with the sixth and eleventh column in another list.
The purpose of this is to compare payouts that match on period between different lists.
Any help whatsoever is appreciated.
Period,IndexId,Id,Date,Payout,,Period,IndexId,Id,Date,Payout
,,,,,,109,75,80010081,03-Sep-2016 05.28.37,239073.74999996735
,,,,,,202,75,80044661,21-May-2013 06.49.16,3.0170290500000052E7
,,,,,,8505,75,80035051,28-May-2013 03.32.41,8835437.250000026
109,128,80010081,03-Sep-2016 05.28.37,239073.749999967,,,,,,
202,128,80044661,21-May-2013 06.49.16,3.01702905000001E7,,,,,,
934,128,80041031,13-Oct-2015 20.22.03,1.55494005E7,,,,,,
Try this one:
var values = File.ReadLines(path).Skip(1)
.Select(x => x.Split(','))
.Where(x => x[0] != string.Empty)
.Select(x => new { Period = x[0], Payout = x[4] })
.ToList();
I'm trying to add a column to the following LINQ expression. I want the column to contain a string concatenation of a text value in a many table called WasteItems. The join would be on "Waste.WasteId = WasteItem.WasteId". My problem is I need to display in a single dynamic column a string such as "EW (5); EX (3)" if there was 8 records in WasteItem and the column containing the 2 character string was called WasteItem.EWC. Hope that makes sense, there must be an efficient way since I realise LINQ is very powerfull. I'm new to it and not sure how to start or go about this:
return from waste in this._db.Wastes
where (from u in _db.UsersToSites.Where(p => p.UserId == userId && p.SystemTypeId == SystemType.W)
select u.SiteId)
.Contains(waste.SiteId)
orderby waste.Entered descending select waste;
THANKS IN ADVANCE
Something like this should do:
wastes.GroupJoin(db.WasteItems, w => w.WastId, wi => wi.WasteId, (w,wi) => new { w, wi })
.AsEnumerable()
.Select(x => new
{
x.w.Name,
Items = string.Join(", ", x.wi.GroupBy(wi => wi.EWC).Select(g => string.Format("{0} ({1})", g.Key, g.Count())))
})
Where wastes is the result from your query. The AsEnumerable() is necessary because Entity Framework can not handle string.Join, so that part must be dealt with in memory.
I could not check the syntax, obviously, but at least it may show you the way to go.