Export csv with Async await in linq select issue - c#

I'm currently stuck with this issue, and really got 0 ideas on how to resolve it. So I'm basically calling the export function after setting this data. Without async and dummy data It works, on implementing this, I'm not able to export anything. This is what I've done by far, and I'm not sure what I am doing wrong here. IF you have any idea, please help me. I can also provide more code if needed.
this is the code:
private async Task<IEnumerable> OnExport(SearchCriteria cr)
{
var sr = await GetResult(cr);
var results = sr.Results;
var exportData = results.Select(async export =>
{
var data = await dataService.GetById(export.Id);
var pAddress = export.ContactInformation?.Addresses.FirstOrDefault(x => x.AddressType == Framework.Contracts.Enums.AddressTypes.Physical);
var mAddress = export.ContactInformation?.Addresses.FirstOrDefault(x => x.AddressType == Framework.Contracts.Enums.AddressTypes.Mailing);
return new
{
LegalBusinessName = export.Name,
DBA = export.DBAName,
BusinessEmail = export.ContactInformation?.PrimaryEmail,
PhonePrimary = export.ContactInformation?.PrimaryPhone,
PhoneTypePrimary = export.ContactInformation?.PrimaryPhoneType,
PhoneSecondary = export.ContactInformation?.SecondaryPhone,
PhoneTypeSecondary = export.ContactInformation?.SecondaryPhoneType,
Website = export.WebAddress,
PhysicalStreetAddress = pAddress?.Address1,
PhysicalAddressLine2 = pAddress?.Address2,
PhysicalCity = pAddress?.City,
PhysicalCounty = pAddress?.County,
PhysicalState = pAddress?.State,
PhysicalZipCode = pAddress?.Zip,
MailingStreetAddress = mAddress?.Address1,
MailingAddressLine2 = mAddress?.Address2,
MailingCity = mAddress?.City,
MailingCounty = mAddress?.County,
MailingState = mAddress?.State,
MailingZipCode = mAddress?.Zip,
SchoolOnboarding = data?.ConfirmedVideo,
SchoolStartDate = data?.SchoolStartDate,
SchoolEndDate = data?.SchoolEndDate,
Grades = string.Join(",", data?.GradeLevels.Select(x => x.GradeLevel)),
IsReligiousSchool = data?.ConfirmedIsReligious,
ReligiousAffiliation = data?.ReligiousAffiliation,
TCSignatureName = export.SignatureName,
TCDateSigned = export.TermsAcceptedOn
};
});
return exportData;
}

It looks like you try to export a System.Type
Definiton of System.Type: Represents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types.
Check if your ExportData has any value that is not a string.

Related

AWS .NET SDK DynamoDB Filter expression by Map values doesn't return any data

I'm trying to load data from DynamoDB.
I use FilterExpression and KeyExpression.
If I search by simple value on the top level everything works fine.
However, when I try to filter records by nested map values, I get 0 records.
CurrentCase is an object, Assignments is Dictionary, Setup is Enum.
Here is my code:
`Expression filterExpression = new ();
filterExpression.ExpressionAttributeNames["#Setup"] = "CurrentCase.Assignments.Setup";
filterExpression.ExpressionAttributeValues[":userId"] = userId;
filterExpression.ExpressionStatement = "#Setup = :userId";`
I tried another way, didn't help. (WHERE CurrentCase.Assignments['Setup'] = 'Id' works in PartyQL):
`Expression filterExpression = new ();
filterExpression.ExpressionAttributeNames["#Setup"] = "CurrentCase.Assignments['Setup']";
filterExpression.ExpressionAttributeValues[":userId"] = userId;
filterExpression.ExpressionStatement = "#Setup = :userId";`
This is how i call query
var queryOperationConfig = new QueryOperationConfig
{
PaginationToken = paginationToken,
Limit = pageSize,
IndexName = GlobalIndexNames.Cases,
KeyExpression = keyExpression,
FilterExpression = filterExpression
};
Search search = _dbContext.GetTargetTable<CaseEntity>().Query(queryOperationConfig);
List<Document> documents = await search.GetNextSetAsync(cancellationToken);
I expect that this request return all records where CurrentCase.Assignments['Setup'] equals userId
Forgive me, im not a .Net coder, but your issue is this:
filterExpression.ExpressionAttributeNames["#Setup"] = "CurrentCase.Assignments.Setup";
You are essentially setting your var #Setup to a String "CurrentCase.Assignments.Setup"
It should be:
ExpressionAttributeNames = new Dictionary<string, string>
{
{ "#CurrentCase", "CurrentCase" },
{ "#Assignments", "Assignments" },
{ "#Setup", "Setup" }
}
filterExpression.ExpressionStatement = "#CurrentCase.#Assignments#Setup = :userId";`
You may need to restructure the example I gave, but you should get the idea.

Adding attributes for faceting with Algolia C#

I have an attribute that I'd like to add for faceting: ApprovalFL. When I add the facet in the Algolia dashboard, it works fine until I have to reindex which ends up deleting my facets in the dashboard for some reason. I was thinking, maybe adding the attribute in my code would resolve that. I found the documentation about this step here: https://www.algolia.com/doc/api-reference/api-parameters/attributesForFaceting/
Here's the C# example they provide:
index.SetSettings(
JObject.Parse(#"{""attributesForFaceting"":[""author"",""filterOnly(category)"",""searchable(publisher)""]}")
);
If I understand correctly, this code needs to go in my backend reindexing code (found in my AdminController.cs):
public async Task<ActionResult> ReIndexData()
{
var algoliaArtistModels = Tools.BuildAlgoliaArtistModels(EntityDataAccess.GetAllAccountInfoes());
var algoliaUnaffiliatedArtistModels = Tools.BuildAlgoliaArtistModels(EntityDataAccess.GetAllUnaffiliatedAccountInfo());
var algoliaSongModels = Tools.BuildAlgoliaSongModels(EntityDataAccess.GetAllAcceptedSongs());
var artistIndexHelper = HttpContext.Application.Get("ArtistIndexHelper") as IndexHelper<ArtistAlgoliaModel>;
var unaffiliatedArtistIndexHelper = HttpContext.Application.Get("UnaffiliatedArtist") as IndexHelper<ArtistAlgoliaModel>;
var songIndexHelper = HttpContext.Application.Get("SongIndexHelper") as IndexHelper<SongAlgoliaModel>;
await artistIndexHelper.OverwriteIndexAsync(algoliaArtistModels);
await unaffiliatedArtistIndexHelper.OverwriteIndexAsync(algoliaUnaffiliatedArtistModels);
await songIndexHelper.OverwriteIndexAsync(algoliaSongModels);
return View("AlgoliaReIndexData");
}
However, I don't think I put index.setSettings in this block of code, what is the best way to set this attribute for faceting in my backend code? The ApprovalFL attribute is stored in my Song indices.
Possibly it should go somewhere here in my Tools.cs?
public static SongAlgoliaModel BuildAlgoliaSongModel(Song song)
{
var model = new SongAlgoliaModel();
var album = EntityDataAccess.GetAlbumByID(song.AlbumID);
model.AccountImageURL = album.AccountInfo.ImageURL;
model.AccountInfoID = album.AccountInfoID;
model.AccountType = album.AccountInfo.CreatorFL == true ? "Creator" : "Artist";
model.AlbumID = song.AlbumID;
model.AlbumName = album.AlbumName;
model.ApprovalFL = song.ApprovalFL;
model.Artist = album.AccountInfo.DisplayName;
model.BPM = song.BPM;
model.Duration = song.Duration;
model.FeaturedArtist = song.Artist;
model.FreeFL = album.FreeFL;
model.ImageURL = album.ImageURL;
model.iTunesURL = album.iTunesURL;
model.LabelName = album.LabelName;
model.LicenseFL = album.LicenseFL;
model.SongID = song.SongID;
model.Title = song.Title;
model.UploadDate = song.UploadDate;
model.URL = song.URL;
model.UserID = album.AccountInfo.UserID;
return model;
}
public static List<SongAlgoliaModel> BuildAlgoliaSongModels(AccountInfo accountInfo)
{
var list = new List<SongAlgoliaModel>();
var songs = EntityDataAccess.GetSongsByUserID(accountInfo.UserID).Where(x => x.ApprovalFL == true).ToList();
foreach(var item in songs)
{
var model = new SongAlgoliaModel();
model.AccountImageURL = item.Album.AccountInfo.ImageURL;
model.AccountInfoID = item.Album.AccountInfoID;
model.AccountType = item.Album.AccountInfo.CreatorFL == true ? "Creator" : "Artist";
model.AlbumID = item.AlbumID;
model.AlbumName = item.Album.AlbumName;
model.ApprovalFL = item.ApprovalFL;
model.Artist = item.Album.AccountInfo.DisplayName;
model.BPM = item.BPM;
model.Duration = item.Duration;
model.FeaturedArtist = item.Artist;
model.FreeFL = item.Album.FreeFL;
model.ImageURL = item.Album.ImageURL;
model.iTunesURL = item.Album.iTunesURL;
model.LabelName = item.Album.LabelName;
model.LicenseFL = item.Album.LicenseFL;
model.SongID = item.SongID;
model.Title = item.Title;
model.UploadDate = item.UploadDate;
model.URL = item.URL;
model.UserID = item.Album.AccountInfo.UserID;
list.Add(model);
}
return list;
}
OK for those using C# .NET it's as simple as adding it to your Global.asax.cs for example for me:
var songIndexHelper = new IndexHelper<SongAlgoliaModel>(algoliaClient, "Song", "SongID");
songIndexHelper.SetSettings(JObject.Parse(#"{""attributesForFaceting"":[""ApprovalFL""]}"));
Why do you have to re-index by the way? Best practice is to set up your index only initially.

How do you reuse mapping functions on Nested entities in Entity Framework?

I have seen multiple questions that are similar to this one but I think my case is slightly different. I'm using EF6 to query the database and I'm using data projection for better queries.
Given that performance is very important on this project I have to make sure to just read the actual fields that I will use so I have very similar queries that are different for just a few fields as I have done this I have noticed repetition of the code so I'm been thinking on how to reuse code this is currently what I Have:
public static IEnumerable<FundWithReturns> GetSimpleFunds(this DbSet<Fund> funds, IEnumerable<int> fundsId)
{
IQueryable<Fund> query = GetFundsQuery(funds, fundsId);
var results = query
.Select(f => new FundWithReturns
{
Category = f.Category,
ExpenseRatio = f.ExpenseRatio,
FundId = f.FundId,
Name = f.Name,
LatestPrice = f.LatestPrice,
DailyReturns = f.FundDailyReturns
.Where(dr => dr.AdjustedValue != null)
.OrderByDescending(dr => dr.CloseDate)
.Select(dr => new DailyReturnPrice
{
CloseDate = dr.CloseDate,
Value = dr.AdjustedValue.Value,
}),
Returns = f.Returns.Select(r => new ReturnValues
{
Daily = r.AdjDaily,
FiveYear = r.AdjFiveYear,
MTD = r.AdjMTD,
OneYear = r.AdjOneYear,
QTD = r.AdjQTD,
SixMonth = r.AdjSixMonth,
ThreeYear = r.AdjThreeYear,
YTD = r.AdjYTD
}).FirstOrDefault()
})
.ToList();
foreach (var result in results)
{
result.DailyReturns = result.DailyReturns.ConvertClosingPricesToDailyReturns();
}
return results;
}
public static IEnumerable<FundListVm> GetFundListVm(this DbSet<Fund> funds, string type)
{
return funds
.Where(f => f.StatusCode == MetisDataObjectStatusCodes.ACTIVE
&& f.Type == type)
.Select(f => new FundListVm
{
Category = f.Category,
Name = f.Name,
Symbol = f.Symbol,
Yield = f.Yield,
ExpenseRatio = f.ExpenseRatio,
LatestDate = f.LatestDate,
Returns = f.Returns.Select(r => new ReturnValues
{
Daily = r.AdjDaily,
FiveYear = r.AdjFiveYear,
MTD = r.AdjMTD,
OneYear = r.AdjOneYear,
QTD = r.AdjQTD,
SixMonth = r.AdjSixMonth,
ThreeYear = r.AdjThreeYear,
YTD = r.AdjYTD
}).FirstOrDefault()
}).OrderBy(f=>f.Symbol).Take(30).ToList();
}
I'm trying to reuse the part where I map the f.Returns so I tried created a Func<> like the following:
private static Func<Return, ReturnValues> MapToReturnValues = r => new ReturnValues
{
Daily = r.AdjDaily,
FiveYear = r.AdjFiveYear,
MTD = r.AdjMTD,
OneYear = r.AdjOneYear,
QTD = r.AdjQTD,
SixMonth = r.AdjSixMonth,
ThreeYear = r.AdjThreeYear,
YTD = r.AdjYTD
};
and then use like this:
public static IEnumerable<FundListVm> GetFundListVm(this DbSet<Fund> funds, string type)
{
return funds
.Where(f => f.StatusCode == MetisDataObjectStatusCodes.ACTIVE
&& f.Type == type)
.Select(f => new FundListVm
{
Category = f.Category,
Name = f.Name,
Symbol = f.Symbol,
Yield = f.Yield,
ExpenseRatio = f.ExpenseRatio,
LatestDate = f.LatestDate,
Returns = f.Returns.Select(MapToReturnValues).FirstOrDefault()
}).OrderBy(f=>f.Symbol).Take(30).ToList();
}
The compiler is ok with it but at runtime, it crashes and says: Internal .NET Framework Data Provider error 1025
I tried to convert the Func into Expression like I read on some questions and then using compile() but It didn't work using AsEnumerable is also not an option because It will query all the fields first which is what I want to avoid.
Am I trying something not possible?
Thank you for your time.
It definitely needs to be Expression<Func<...>>. But instead of using Compile() method (not supported), you can resolve the compile time error using the AsQueryable() method which is perfectly supported (in EF6, the trick doesn't work in current EF Core).
Given the modified definition
private static Expression<Func<Return, ReturnValues>> MapToReturnValues =
r => new ReturnValues { ... };
the sample usage would be
Returns = f.Returns.AsQueryable().Select(MapToReturnValues).FirstOrDefault()

Assigning values from a object with known properties

Im writing a class with witch creates a overview over the employees phones.
I am getting the info form Activesync object containing phones as children.
This is my current code. It works if the child dont contain any nulls.
foreach (DirectoryEntry child in directoryObject.Children)
{
var activeSyncPhone = new ActiveSync.Phone();
activeSyncPhone.Cn = child.Properties["cn"].Value.ToString(); //string
activeSyncPhone.DistinguishedName = child.Properties["distinguishedName"].Value.ToString(); //sting
activeSyncPhone.InstanceType = (int)child.Properties["instanceType"].Value; //int
activeSyncPhone.WhenCreated = (DateTime)child.Properties["whenCreated"].Value; //datetime
activeSyncPhone.WhenChanged = (DateTime)child.Properties["whenChanged"].Value; //datetime
activeSyncPhone.Name = child.Properties["name"].Value.ToString(); //string
activeSyncPhone.ObjectCategory = child.Properties["objectCategory"].Value.ToString(); //string
activeSyncPhone.MsExchFirstSyncTime = (DateTime)child.Properties["msExchFirstSyncTime"].Value;//datetime
activeSyncPhone.MsExchDeviceEASVersion = child.Properties["msExchDeviceEASVersion"].Value.ToString();//string
activeSyncPhone.MsExchDeviceFriendlyName = child.Properties["msExchDeviceFriendlyName"].Value.ToString(); //string
activeSyncPhone.MsExchDeviceAccessState = (ActiveSync.Phone.DeviceAccessState)child.Properties["msExchDeviceAccessState"].Value; //int
activeSyncPhone.MsExchDeviceID = child.Properties["msExchDeviceID"].Value.ToString(); //string
activeSyncPhone.MsExchDeviceType = child.Properties["msExchDeviceType"].Value.ToString(); //string
try
{
activeSyncPhone.MsExchDeviceIMEI = child.Properties["msExchDeviceIMEI"]?.Value.ToString(); //string
}
catch
{
activeSyncPhone.MsExchDeviceIMEI = "Could not find IMEI";
}
activeSyncPhone.MsExchDeviceUserAgent = child.Properties["msExchDeviceUserAgent"].Value.ToString(); //string
activeSyncPhone.MsExchVersion = child.Properties["msExchVersion"].Value.ToString(); //string
activeSyncPhone.MsExchDeviceAccessStateReason = (ActiveSync.Phone.DeviceAccessStateReason)child.Properties["msExchDeviceAccessStateReason"].Value; //string
activeSyncPhone.MsExchUserDisplayName = child.Properties["msExchUserDisplayName"].Value.ToString(); //string
activeSyncPhone.MsExchDeviceModel = child.Properties["msExchDeviceModel"].Value.ToString(); //string
activeSyncPhone.MsExchDeviceOS = child.Properties["msExchDeviceOS"].Value.ToString(); //string
activeSyncPhone.ObjectGUID = child.Properties["objectGUID"].Value.ToString(); //string
activeSyncUnits.PhoneList.Add(activeSyncPhone);
child.Close();
}
directoryObject.Close();
I was wondering if there was any way to make this a bit more robust. I was looking into setting the ActiveSyncPhone's propertys dynamically and then with a list set all the properties. But C# is a strongly-typed language and I figured id take advantage of the type safety and performance advantages that accompany that aspect. I think there might be a better way then checking every child.property for null with if statements? and aslo is there a better way for getting the child properties?
You could create a generic function for that:
// you'll have to figure out the type of the `child.Properties`
public static T GetValue<T>(TypeOfChildProperties properties, string name, T defaultValue = default(T))
{
var value = properties[name];
if (value == null)
return defaultValue;
return (T)value;
// if you have some cast problems, you could use this:
return (T)Convert.ChangeType(value, typeof(T));
}
activeSyncPhone.Cn = GetValue<string>(child.Properties, "cn");
activeSyncPhone.DistinguishedName = GetValue<string>(child.Properties, "distinguishedName");
activeSyncPhone.InstanceType = GetValue<int>(child.Properties, "instanceType");
activeSyncPhone.MsExchDeviceIMEI = GetValue<string>(child.Properties, "msExchDeviceIMEI", "Could not find IMEI");

Return Entity Framework result as JSON

I want to get my linq query result back as json format. I have been searching for hours.
Here is my code :
public IEnumerable<callersW> GetAllCallersF()
{
testCDREntities1 context = this.CurrentDataSource;
var query = (
from oneCaller in CurrentDataSource.TestTables
select new
{
Created = oneCaller.Created,
Answered = oneCaller.Answered,
Destroyed = oneCaller.Destroyed,
CallerID = oneCaller.CallerId,
CalledID = oneCaller.CalledId,
DisconnectionCode = oneCaller.DisconnectionCode,
RTP_Caller_G107MOS = oneCaller.RTP_Caller_G107MOS,
RTP_Caller_LostPackets = oneCaller.RTP_Caller_LostPackets,
RTP_Caller_MaxRfc3550Jitter = oneCaller.RTP_Caller_MaxRfc3550Jitter,
RTP_Caller_MeanRfc3550Jitter = oneCaller.RTP_Caller_MeanRfc3550Jitter,
RTP_Called_G107MOS = oneCaller.RTP_Called_G107MOS,
RTP_Called_LostPackets = oneCaller.RTP_Called_LostPackets,
RTP_Called_MaxRfc3550Jitter = oneCaller.RTP_Called_MaxRfc3550Jitter,
RTP_Called_MeanRfc3550Jitter = oneCaller.RTP_Called_MeanRfc3550Jitter,
}).ToList()
.Select(x => new callersW
{
Created = Convert.ToDateTime(x.Created),
Answered = Convert.ToDateTime(x.Answered),
Destroyed = Convert.ToDateTime(x.Destroyed),
CallerID = x.CallerID,
CalledID = x.CalledID,
DisconnectionCode = Convert.ToInt32(x.DisconnectionCode),
RTP_Caller_G107MOS = Convert.ToDouble(x.RTP_Caller_G107MOS),
RTP_Caller_LostPackets = Convert.ToDouble(x.RTP_Caller_LostPackets),
RTP_Caller_MaxRfc3550Jitter = Convert.ToDouble(x.RTP_Caller_MaxRfc3550Jitter),
RTP_Caller_MeanRfc3550Jitter = Convert.ToDouble(x.RTP_Caller_MeanRfc3550Jitter),
RTP_Called_G107MOS = Convert.ToDouble(x.RTP_Called_G107MOS),
RTP_Called_LostPackets = Convert.ToDouble(x.RTP_Called_LostPackets),
RTP_Called_MaxRfc3550Jitter = Convert.ToDouble(x.RTP_Called_MaxRfc3550Jitter),
RTP_Called_MeanRfc3550Jitter = Convert.ToDouble(x.RTP_Called_MeanRfc3550Jitter)
}).ToList();
return query;
}
Can somebody help me with this ?
Add to your project JSON.NET and serialize object eg:
string json = JsonConvert.SerializeObject(query);
More examples here.

Categories

Resources