Merging Multiple IQueryables together - Silverlight - silverlight

Peeps,
Im a born again virgin to Silverlight so please bear with me. I have two seperate DomainServices, pointing to two different SQL Database Servers. Within these Domain Services I have setup some IQueryables in each Domain Service.
I need to merge two IQueryables together from seperate DomainServices. Im sure this has got to be do-able.
Below are the two domain services.
I want to merge GetCustomCallLogs (from HEATLiveDomainService) together with GetTblCallsLogged (from HeatDomainService). The key in GetCustomCallLogs would be the CallID and the key in GetTblCallsLogged would be the RecID.
If its possible to do i understand i would need to create a new type to take into account any fields from the two joined tables.
Hopefully ive explained my scenario, and im not being dumb.
Thanks in advance
public class HEATLIVEDomainService : LinqToEntitiesDomainService<HeatIT9_LiveEntities>
{
// TODO:
// Consider constraining the results of your query method. If you need additional input you can
// add parameters to this method or create additional query methods with different names.
// To support paging you will need to add ordering to the 'Assignees' query.
public IQueryable<Assignee> GetAssignees()
{
return this.ObjectContext.Assignees;
}
// TODO:
// Consider constraining the results of your query method. If you need additional input you can
// add parameters to this method or create additional query methods with different names.
// To support paging you will need to add ordering to the 'CallLogs' query.
public IQueryable<CallLog> GetCallLogs()
{
// return this.ObjectContext.CallLogs.Where(c => DateTime.Parse(c.ClosedDate).Year == 2012 && c.CallStatus == "Closed" && c.ClosedBy.Length > 0);
return this.ObjectContext.CallLogs.Where(c => c.CallStatus == "Closed" && c.ClosedDate.Substring(0, 4).Equals("2013"));
}
public IQueryable<CallLog> GetCallLogsLastThisYear()
{
return this.ObjectContext.CallLogs.Where(c => c.CallStatus == "Closed" && (c.ClosedDate.Substring(0, 4).Equals("2012") || c.ClosedDate.Substring(0, 4).Equals("2013")));
}
public IQueryable<CustomCallLog> GetCustomCallLogs(string year)
{
var result = from i in this.ObjectContext.CallLogs
join p in this.ObjectContext.Assignees on i.ClosedBy equals p.LoginID
where i.CallStatus == "Closed" && i.ClosedDate.Substring(0, 4) == year
select new CustomCallLog
{
Score =
CallLog = i.CallID,
Name = p.Assignee1,
Yr = year,
Mth = i.ClosedDate.Substring(5, 2),
GroupName = p.GroupName
};
return result;
}
public IQueryable<CustomClosedJobs> GetCustomClosedJobs()
{
var result = from i in this.ObjectContext.CallLogs
where i.CallStatus == "Closed" && i.ClosedDate.Substring(0, 4) =="2012"
group i by i.ClosedDate.Substring(5,2) into y
select new CustomClosedJobs
{
Type = "heat",
ClosedCalls = y.Count(),
Mth =y.Key
};
return result;
}
// TODO:
// Consider constraining the results of your query method. If you need additional input you can
// add parameters to this method or create additional query methods with different names.
// To support paging you will need to add ordering to the 'Subsets' query.
public IQueryable<Subset> GetSubsets()
{
return this.ObjectContext.Subsets;
}
}
public class HEATDomainService : LinqToEntitiesDomainService<FEEDBACKPRDEntities1>
{
// TODO:
// Consider constraining the results of your query method. If you need additional input you can
// add parameters to this method or create additional query methods with different names.
// To support paging you will need to add ordering to the 'qryStoringLogs' query.
public IQueryable<qryStoringLog> GetQryStoringLogs()
{
return this.ObjectContext.qryStoringLogs.OrderBy(e => e.monthno);
}
public IQueryable<tblStoringLog> GetTop100Logs()
{
return this.ObjectContext.tblStoringLogs.OrderByDescending(e => e.responsetime).Take(100);
}
public IQueryable<tblStoringLog> GetLogCount(DateTime s, DateTime e)
{
return this.ObjectContext.tblStoringLogs.Where(x => x.responsetime >= s && x.responsetime <= e);
}
public IQueryable<qryStoringLog> GetLogs(int year, int mth)
{
return this.ObjectContext.qryStoringLogs.Where(e => e.Month.Equals(mth) && e.yr.Equals(year));
}
public IQueryable<qryStoringLog> GetLogsForYear(int year)
{
return this.ObjectContext.qryStoringLogs.Where(e => e.yr.Equals(year)).OrderBy(e => e.monthno);
}
public DateTime GetFirstDate()
{
return (DateTime)this.ObjectContext.tblStoringLogs.OrderBy(e => e.responsetime).First().responsetime;
}
public DateTime GetLastDate()
{
return (DateTime)this.ObjectContext.tblStoringLogs.OrderByDescending(e => e.responsetime).First().responsetime;
}
public IQueryable<tblCallLogged> GetTblCallLoggeds()
{
return this.ObjectContext.tblCallLoggeds;
}
public IQueryable<tblStoringLog> GetTblStoringLogs()
{
return this.ObjectContext.tblStoringLogs;
}
[Query(IsComposable = false)]
public IQueryable<qryStoringLog> GetTblStoringLogsStatus(int statusid)
{
return this.ObjectContext.qryStoringLogs.Where(e => e.statusid == statusid);
}
public IQueryable<stResponsesLife_Result> LifeTimeResponses()
{
return this.ObjectContext.stResponsesLife().AsQueryable();
}
public IEnumerable<CustomLifetime> GetCustomLifeTime()
{
var result = from i in this.ObjectContext.stResponsesLife().ToList()
select new CustomLifetime
{
Dates = i.Dates,
Vals = (int)i.Vals
};
return result;
}
public int GetAllResponses()
{
return this.ObjectContext.qryStoringLogs.Count();
}
}
Caveats:
Cannot have linked servers, so doing it at the source (SQL Server) is out of the question.
Cannot create a SP and use OpenQuery (well i could but i want to learn to do it correctly), as im sure this isnt the correct way of doing it.

You may need a third type to wrap the two different types returned from each service, but this depends upon how you want to use it.
An ItemsControl wont care about what types are in the collection (Listbox, combobox etc), but GridView type controls might be picky.
Using linq, both lists can simply be merged. Something like the following should do the trick:
collection1.Select(o => (object)o).Concat(collection2.Select(o => (object)o));
The Select and Cast is so that an appropriate generic collection is created by the query.
This can be adjusted to incorporate conversion to your wrapper type too. ie Instead of casting to Object, just return a new instance of your wrapper class instead.
You can even use a Union if appropriate.
Putting it all together:
collection1.Select(o => new MyWrapperType(o))
.Union(collection2.Select(o => new MyWrapperType(o)));

Related

How to search for empty strings in a text field with Entity Framework?

I'd like to know how can I search for empty strings when I'm using a text type field with Entity Framework.
I've looked the SQL query that Entity is generating and It's using LIKE to compare because It's searching in a text type field, so when I use .Equals(""), == "", == string.Empty, .Contains(""), .Contains(string.Empty), and everything else, It's returning all results because it sql query is like '' and the == command throws exception because It uses the = command that is not valid with text type field.
When I try to use .Equals(null), .Contains(null), == null, It returns nothing, because It is generating FIELD ISNULL command.
I already tried the .Lenght == 0 but It throws an exception...
This works for me:
public class POCO
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
static void Main(string[] args)
{
var pocos = new List<POCO>
{
new POCO { Id = 1, Name = "John", Description = "basic" },
new POCO { Id = 2, Name = "Jane", Description = "" },
new POCO { Id = 3, Name = "Joey", Description = string.Empty }
};
pocos.Where(x => x.Description == string.Empty)
.ToList()
.ForEach(x => Console.WriteLine($"{x.Id} {x.Name} {x.Description}"));
}
However the issue MAY BE that your T4 generated object is not fully realized with data you can use, if you are using Entity Framework. EG the translation from the database is not populating objects to interrogate correctly. I would just do an operation like this to see:
using (var context = new YOURCONTEXTNAME())
{
var persons = context.YOURDATABASEOBJECT.ToList();
persons.ForEach(x => Console.WriteLine($"{x.COLUMNINQUESTION}"));
}
If you are successfully having data in it, it should be retrieved. I would not use text if possible. Use a varchar(max) nvarchar(max) xml, whatever text will be deprecated eventually and is bad form so to speak to continue using at this point.
EDIT
Okay I see, the answer is you cannot interogate the object until it is fully realized when it is text. I did a test on my local database and created a context and tested it and sure enough you cannot do a '== string.empty', '== ""', or 'String.IsNullOrEmpty()' on a text. However you can do it once the object is materialized in a realized object. EG:
// Won't work as context does not understand type
//var persons = context.tePersons.Where(x => x.Description == string.Empty).ToList();
//Works fine as transformation got the object translated to a string in .NET
var start = context.tePersons.ToList();
var persons = start.Where(x => x.Description == String.Empty).ToList();
This poses a problem obviously as you need to get ALL your data potentially before performing a predicate. Not the best means by any measure. You could do a sql object for this instead then to do a function, proc, or view to change this.

Retrieving every field of a database row as object in zend framework 2

I know we have result set to get a row as object But How can I get every field as a separate object ? consider of this database row :
user_id address_id product_id shop_id
5 3 134 2
I want to retrieve and save the row as follows :
userEntity AddressEntity ProductEntity ShopEntity
This is not how the TableDataGateway is supposed to be used, since what you are looking for are more complex features such as the ones of Doctrine 2 ORM and similar data-mappers.
Here is one possible solution to the problem, which involves using a custom hydrator (docs). My example is simplified, but I hope it clarifies how you are supposed to build your resultset.
First, define your entities (I'm simplifying the example assuming that UserEntity is the root of your hydration):
class UserEntity {
/* fields public for simplicity of the example */
public $address;
public $product;
public $shop;
}
class AddressEntity { /* add public fields here for simplicity */ }
class ProductEntity { /* add public fields here for simplicity */ }
class ShopEntity { /* add public fields here for simplicity */ }
Then, build hydrators specific for the single entities:
use Zend\Stdlib\Hydrator\HydratorInterface as Hydrator;
class AddressHydrator implements Hydrator {
// #TODO: implementation up to you
}
class ProductHydrator implements Hydrator {
// #TODO: implementation up to you
}
class ShopHydrator implements Hydrator {
// #TODO: implementation up to you
}
Then we aggregate these hydrators into one that is specifically built to hydrate a UserEntity:
class UserHydrator extends \Zend\Stdlib\Hydrator\ObjectProperty {
public function __construct(
Hydrator $addressHydrator,
Hydrator $productHydrator,
Hydrator $shopHydrator
) {
$this->addressHydrator = $addressHydrator;
$this->productHydrator = $productHydrator;
$this->shopHydrator = $shopHydrator;
}
public function hydrate(array $data, $object)
{
if (isset($data['address_id'])) {
$data['address'] = $this->addressHydrator->hydrate($data, new AddressEntity());
}
if (isset($data['product_id'])) {
$data['product'] = $this->productHydrator->hydrate($data, new ProductEntity());
}
if (isset($data['shop_id'])) {
$data['shop'] = $this->shopHydrator->hydrate($data, new ShopEntity());
}
return parent::hydrate($data, $object);
}
}
Now you can use it to work with your resultset. Let's define the service for your UserEntityTableGateway:
'UserEntityTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new UserHydrator());
return new TableGateway('user', $dbAdapter, null, $resultSetPrototype);
},
These are all simplified examples, but they should help you understanding how powerful hydrators can be, and how you can compose them to solve complex problems.
You may also check the chapters in the documentation about the Aggregate Hydrator and Hydration Strategies, which were designed specifically to solve your problem.

How to know the order of update with Domain context SubmitChanges?

Suppose I have 3 entities generated from EF, say tab1, tab2 and tab3. In SL app, I call SubmitChanges to save data to DB, all changes will be process by WCF and EF automatically.
Question is: how can I know the order of Update operation in Database?
I need to know this because I have triggers on those tables and need to know the order of the updating.
One thing you can do is to override the PeristChangeSet() in your DomainService and manually control the order of saves. Just do nothing in your regular update/insert statements. Here's some pseudocode for a saving a document exmmple to explain my answer:
[Insert]
public void InsertDocument(MyDocument objDocument) { }
[Update]
public void UpdateDocument(MyDocument objDocument) { }
protected override bool PersistChangeSet()
{
try {
// have to save document first to get its id....
MyDocument objDocumentBeingSaved = null;
foreach (ChangeSetEntry CSE in ChangeSet.ChangeSetEntries.Where(i => i.Entity is MyDocument)) {
var changedEntity = (MyDocument)CSE.Entity;
objDocumentBeingSaved = documentRepository.SaveDocument(changedEntity);
break; // only one doc
}
if (objDocumentBeingSaved == null)
throw new NullReferenceException("CreateDocumentDomainService.PersistChangeSet(): Error saving document information. Document is null in entity set.");
// save document assignments after saving document object
foreach (ChangeSetEntry CSE in ChangeSet.ChangeSetEntries.Where(i => i.Entity is DocumentAssignment)) {
var changedEntity = (DocumentAssignment)CSE.Entity;
changedEntity.DocumentId = objDocumentBeingSaved.Id;
changedEntity.Id = documentRepository.SaveDocumentAssignment(objDocumentBeingSaved, changedEntity);
}
// save line items after saving document assignments
foreach (ChangeSetEntry CSE in ChangeSet.ChangeSetEntries.Where(i => i.Entity is LineItem)) {
var changedEntity = (LineItem)CSE.Entity;
changedEntity.DocumentId = objDocumentBeingSaved.Id;
changedEntity.Id = documentRepository.SaveLineItem(objDocumentBeingSaved, changedEntity);
}
documentRepository.GenerateDocumentNumber(objDocumentBeingSaved.Id);
}
catch {
// ....
throw;
}
return false;
}

Can't add sub-entities in Linq2Sql

I have 2 entity types Buyers and BuyerOrders. Buyer can contain multiple BuyerOrders and list of BuyerOrders can be edited by users. When I want to add some BuyerOrders I call method:
private static void CreateOrdersForBuyer(Buyer buyer, int[] selectedLeadTypes)
{
foreach (var order in selectedLeadTypes.Select(leadTypeId => new BuyerOrder
{
Buyer = buyer,
OfferTypeID = (int) OfferTypes.Referral,
Price = 1,
RegistrationDate = DateTime.Now,
StatusID = (int) BuyerOrderStatus.Pending,
LeadtypeID = leadTypeId,
Profit = 1
}))
{
buyer.BuyerOrders.Add(order);
}
}
And then save the buyer:
GenericRepository.Instance.Save(buyer);
Save method:
public virtual void Save<T>(T value) where T : class
{
//LegalLeadsDataContext.Instance = null;
LegalLeadsDataContext.Instance.GetTable<T>().Attach(value);
LegalLeadsDataContext.Instance.Refresh(RefreshMode.KeepCurrentValues, value);
LegalLeadsDataContext.Instance.SubmitChanges();
}
After that I see no new BuyerOrders in Database. What am I doing wrong?
From the MSDN documentation
KeepCurrentValues: Forces the Refresh method to swap the original value with the values retrieved from the database. No current value is modified.
Whereas the KeepChanges
KeepChanges: Forces the Refresh method to keep the current value that has been changed, but updates the other values with the database values.
Also, the Attach method, use the overload that allows you to specify "as modified".
So in your code, it would appear that possibly you need to change
public virtual void Save<T>(T value) where T : class
{
//LegalLeadsDataContext.Instance = null;
LegalLeadsDataContext.Instance.GetTable<T>().Attach(value, true);
LegalLeadsDataContext.Instance.Refresh(RefreshMode.KeepChanges, value);
LegalLeadsDataContext.Instance.SubmitChanges();
}

salesforce SOQL : query to fetch all the fields on the entity

I was going through the SOQL documentation , but couldn't find query to fetch all the field data of an entity say , Account , like
select * from Account [ SQL syntax ]
Is there a syntax like the above in SOQL to fetch all the data of account , or the only way is to list all the fields ( though there are lot of fields to be queried )
Create a map like this:
Map<String, Schema.SObjectField> fldObjMap = schema.SObjectType.Account.fields.getMap();
List<Schema.SObjectField> fldObjMapValues = fldObjMap.values();
Then you can iterate through fldObjMapValues to create a SOQL query string:
String theQuery = 'SELECT ';
for(Schema.SObjectField s : fldObjMapValues)
{
String theLabel = s.getDescribe().getLabel(); // Perhaps store this in another map
String theName = s.getDescribe().getName();
String theType = s.getDescribe().getType(); // Perhaps store this in another map
// Continue building your dynamic query string
theQuery += theName + ',';
}
// Trim last comma
theQuery = theQuery.subString(0, theQuery.length() - 1);
// Finalize query string
theQuery += ' FROM Account WHERE ... AND ... LIMIT ...';
// Make your dynamic call
Account[] accounts = Database.query(theQuery);
superfell is correct, there is no way to directly do a SELECT *. However, this little code recipe will work (well, I haven't tested it but I think it looks ok). Understandably Force.com wants a multi-tenant architecture where resources are only provisioned as explicitly needed - not easily by doing SELECT * when usually only a subset of fields are actually needed.
You have to specify the fields, if you want to build something dynamic the describeSObject call returns the metadata about all the fields for an object, so you can build the query from that.
I use the Force.com Explorer and within the schema filter you can click the checkbox next to the TableName and it will select all the fields and insert into your query window - I use this as a shortcut to typeing it all out - just copy and paste from the query window. Hope this helps.
In case anyone was looking for a C# approach, I was able to use reflection and come up with the following:
public IEnumerable<String> GetColumnsFor<T>()
{
return typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
.Where(x => !Attribute.IsDefined(x, typeof(System.Xml.Serialization.XmlIgnoreAttribute))) // Exclude the ignored properties
.Where(x => x.DeclaringType != typeof(sObject)) // & Exclude inherited sObject propert(y/ies)
.Where(x => x.PropertyType.Namespace != typeof(Account).Namespace) // & Exclude properties storing references to other objects
.Select(x => x.Name);
}
It appears to work for the objects I've tested (and matches the columns generated by the API test). From there, it's about creating the query:
/* assume: this.server = new sForceService(); */
public IEnumerable<T> QueryAll<T>(params String[] columns)
where T : sObject
{
String soql = String.Format("SELECT {0} FROM {1}",
String.Join(", ", GetColumnsFor<T>()),
typeof(T).Name
);
this.service.QueryOptionsValue = new QueryOptions
{
batchsize = 250,
batchSizeSpecified = true
};
ICollection<T> results = new HashSet<T>();
try
{
Boolean done = false;
QueryResult queryResult = this.service.queryAll(soql);
while (!finished)
{
sObject[] records = queryResult.records;
foreach (sObject record in records)
{
T entity = entity as T;
if (entity != null)
{
results.Add(entity);
}
}
done &= queryResult.done;
if (!done)
{
queryResult = this.service.queryMode(queryResult.queryLocator);
}
}
}
catch (Exception ex)
{
throw; // your exception handling
}
return results;
}
For me it was the first time with Salesforce today and I came up with this in Java:
/**
* #param o any class that extends {#link SObject}, f.ex. Opportunity.class
* #return a list of all the objects of this type
*/
#SuppressWarnings("unchecked")
public <O extends SObject> List<O> getAll(Class<O> o) throws Exception {
// get the objectName; for example "Opportunity"
String objectName= o.getSimpleName();
// this will give us all the possible fields of this type of object
DescribeSObjectResult describeSObject = connection.describeSObject(objectName);
// making the query
String query = "SELECT ";
for (Field field : describeSObject.getFields()) { // add all the fields in the SELECT
query += field.getName() + ',';
}
// trim last comma
query = query.substring(0, query.length() - 1);
query += " FROM " + objectName;
SObject[] records = connection.query(query).getRecords();
List<O> result = new ArrayList<O>();
for (SObject record : records) {
result.add((O) record);
}
return result;
}
I used following to get complete records-
query_all("Select Id, Name From User_Profile__c")
To get complete fields of record, we have to mention those fields as mentioned here-
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select.htm
Hope will help you !!!

Resources