I got some newCity:
ObservCity newCity = new ObservCity ()
{
id = localCityId,
entries = somecity.ToArray()
};
Now I load saved city:
ObservCity loadOldCity = await myService.loadCity(id);
How to add newCity.entries to loadOldCity.entries?
This don't work
newCity.entries.Concat(loadOldCity.entries);
//save newCity + loadOldCity
myService.saveCity(newCity);
You have not posted any class declarations, so I will take a guess and say entries is either an IEnumerable or an array, based on your current code. I will also assume the whole property is read-only. So you just create a new instance:
var updatedCity = new ObservCity
{
id = localCityId,
entries = newCity.entries.Concat(loadOldCity.entries).ToArray()
};
myService.saveCity(updatedCity);
You can add a range of items with the AddRange() method.
newCity.entries.AddRange(loadOldCity?.entries);
Related
I can't find an example of this here or in the Acumatica sample code. Sending single attribute values works fine, but I can't find a way to send multi-select ones. They are returned as a comma-separated list in a string value, but sending them that way doesn't work. Also, sending them as multiple instances of single values doesn't work.
Here's what I've tried. (In the actual code I'm sending some other single attributes in the list, as well, but those work fine.)
// this results in nothing being set for the attribute
string interestedIn = "Brochure, Contact, Collecting small stones";
List<Acumatica.AttributeDetail> attributes = new List<Acumatica.AttributeDetail>();
attributes.Add(
new Acumatica.AttributeDetail {
Attribute = new Acumatica.StringValue { Value = "Interested in" },
Value = new Acumatica.StringValue { Value = interestIn }
}
);
custAdd.Attributes = attributes.ToArray();
// this results in the last item in the list being set for the attribute
string interestedIn = "Brochure, Contact, Collecting small stones";
List<Acumatica.AttributeDetail> attributes = new List<Acumatica.AttributeDetail>();
string[] interests = Convert.ToString(interestedIn).Split(',');
foreach (string interest in interests) {
attributes.Add(
new Acumatica.AttributeDetail {
Attribute = new Acumatica.StringValue { Value = "Interested in" },
Value = new Acumatica.StringValue { Value = interest.Trim() }
}
);
};
custAdd.Attributes = attributes.ToArray();
From the source
MappedCustomer obj = bucket.Customer;
Core.API.Customer impl = obj.Local;
impl.Attributes = impl.Attributes ?? new List<AttributeValue>();
AttributeValue attribute = new AttributeValue();
attribute.AttributeID = new StringValue() { Value = attributeID };
attribute.ValueDescription = new StringValue() { Value = attributeValue?.ToString() };
impl.Attributes.Add(attribute);
Some subtle differences here. Also, I wonder if the .ToArray() call is necessary.
I'm new in developing acumatica I am stuck at getting the value of a custom TextEdit field that I created. I can get all of the built-in field value through this code
InventoryItem items = (InventoryItem)Base.Item.Cache.Current;
but I cannot get the one that I have created at acumatica customization
here is the field I want to get
https://i.stack.imgur.com/gPln4.png
I already tried
InventoryItem items = (InventoryItem)Base.ItemSettings.Cache.Current;
var shortdesc = items.UsrShortDescription;
But it's not working and does not show the value inside the textbox
thank you in advance for helping
InventoryItem items = (InventoryItem)Base.ItemSettings.Current;
var itemExt = PXCache<InventoryItem>.GetExtension<InventoryItemExt>(items);
var shortdesc = itemExt.UsrShortDescription;
Vardan showed one way, for completeness of picture want to show another as well:
InventoryItem items = (InventoryItem)Base.ItemSettings.Current;
var itemExt = items.GetExtension<InventoryItemExt>();
This is an example of getting value from a non-extension field. I did not use extension DAC to add the Gift card field to the store setup screen.
In a method I need to get the value of that field. I should check whether the order contains Gift card item or not.
public static bool GiftcardName(OrderModel orders, BZWoocommerceStore store)
{
// "ZGift CArd W" => "giftcard"
string wooCommName = string.Empty;
string wooCommNameNoSpases = string.Empty;
bool containsGiftcardName = false;
bool isGiftcard = false;
foreach (OrderLineModel line in orders.LineItems)
{
string gNameInAcumatica = store.GiftcardIdentifier;
string gNameInAcumaticaWithoutSpaces = gNameInAcumatica.Replace(" ", "");
wooCommName = line.Name; //pattern
wooCommNameNoSpases = wooCommName.Replace(" ", "");
//wooCommNameNoSpases = new string(wooCommName.ToCharArray()
// .Where(c => !Char.IsWhiteSpace(c))
// .ToArray());
//woCommNameNoUperCase= wooCommNameNoSpases.ToLower();
//isGiftcardName= woCommNameNoUperCase.Contains(gName);
//containsGiftcardName = wooCommNameNoSpases.Contains(gName);
containsGiftcardName = Regex.IsMatch(wooCommNameNoSpases, gNameInAcumaticaWithoutSpaces, RegexOptions.IgnoreCase);
if(containsGiftcardName)
{
isGiftcard = true;
}
}
return isGiftcard;
}
So, when I call this method I give to that 2 arguments, orders and store.
The store argument was created in this way.
public PXSelect<BZWoocommerceOrder> Order;
In an action method I wrote this.
string storeCode = this.Order.Current.StoreCode;
BZWoocommerceStore store = PXSelect<BZWoocommerceStore, Where<BZWoocommerceStore.storeCode, Equal<Required<BZWoocommerceStore.storeCode>>>>.Select(this, storeCode);
My GiftcardName() method sees the value of original field. Writing "Original" I mean that you do not use any technique like this one.
BZSOOrderExt rowExt = sender.GetExtension<BZSOOrderExt>(row);
List<ListOrRecordRef> List = new List<ListOrRecordRef>();
ListOrRecordRef RecordRefItem = new ListOrRecordRef();
RecordRefItem.name = "American Express";
RecordRefItem.internalId = "898";
RecordRefItem.typeId = "394";
List.Add(RecordRefItem);
rec.customFieldList = List.ToArray();
WriteResponse response = service.add(rec);
The code is used to add multiselect option of vendor. ex : american express
First of all like Heinz Siahaan said: 'List' is a keyword in C# so you can't create variable with this name.
Second:
ListOrRecordRef RecordRefItem = new ListOrRecordRef();
I'm not sure but name of this method suggest that this line of code creates list of records not one item so you can't use something like this:
RecordRefItem.name = "American Express";
but you should try :
RecordRefItem[i].name = "American Express";
where i is and index of element, but before access it you must create it
found a way its working fine://Note that for multi select option to set we have to take two class:ListOrRecordRef mention the id of the 898:American express&
//SelectCustomFieldRef to mention the field
ListOrRecordRef recordRefItem = new ListOrRecordRef();
recordRefItem.internalId = "898";
SelectCustomFieldRef scfr = new SelectCustomFieldRef();
scfr.scriptId = "custrecord_from_so_customer";
scfr.value = recordRefItem;//set the object value to the mentioned field
customFieldArray[1] = scfr;
rec.customFieldList = customFieldArray
This question already has answers here:
How can I add an item to a IEnumerable<T> collection?
(17 answers)
Closed 9 years ago.
I have the following code:
var contentTypes =
(
from contentType in this._contentTypeService.GetContentTypes()
select new
{
id = contentType.ContentTypeId,
name = contentType.Name
}
);
How can I add another element to contentTypes with an id of 99 and a name of "All"?
I was trying to use contentTypes.Add(
but that does not seem to be allowed with intellisense.
You can't add to an IEnumerable<T>. IEnumerable<T>s represent sequences that you can iterate over; they do not represent a collection that you can add to. What you can do is concatenate to the end of your sequence though, obtaining a new sequence:
var sequence = contentTypes.Concat(
new[] {
new { id = 99, name = "All" }
}
);
Now, if you iterate over sequence, you'll first see the elements of contentTypes streamed to you, and then the final item will be the appended item new { id = 99, name = "All" }.
You can concatenate your new values to the end of the IEnumerable<>.
var contentTypes =
(
from contentType in new[]{new {ContentTypeId = 1, Name="TEST"}}
select new
{
id = contentType.ContentTypeId,
name = contentType.Name
}
).Concat(new[]{new {id = 99, name="All"}});
The resulting IEnumerable will end with 99/All
If you use contentTypes.ToList() you can then add to that list, however doing so will create a new instance of a collection, so your not actually modifying the source collection.
Try this -
var contentTypes =
(
from contentType in this._contentTypeService.GetContentTypes()
select new
{
id = contentType.ContentTypeId,
name = contentType.Name
}
).ToList();
As you have converted contentTypes to a List, it should allow you to add a new item to it.
First, you cannot use IList.Add on an IEnumerable<T>. So you need to create a new collection.
You are selecting an anonymous type, use Concat to add a fixed anynymous type to your query:
var allTypes = new[]{new { id = 99, name = "All" }}; // creates a fixed anonymous type as `IEnumerable<T>`
var contentTypes = from contentType in this._contentTypeService.GetContentTypes()
select new
{
id = contentType.ContentTypeId,
name = contentType.Name
};
var result = allTypes.Concat(contentTypes).ToList(); // concat
I think I understand returning records of an anonymous type from But in this I want to create NEW CatalogEntries, and set them from the values selected. (context is a Devart LinqConnect database context, which lets me grab a view).
My solution works, but it seems clumsy. I want to do this in one from statement.
var query = from it in context.Viewbostons
select it;
foreach (GPLContext.Viewboston item in query)
{
CatalogEntry card = new CatalogEntry();
card.idx = item.Idx;
card.product = item.Product;
card.size = (long)item.SizeBytes;
card.date = item.Date.ToString();
card.type = item.Type;
card.classification = item.Classification;
card.distributor = item.Distributor;
card.egplDate = item.EgplDate.ToString();
card.classificationVal = (int)item.ClassificationInt;
card.handling = item.Handling;
card.creator = item.Creator;
card.datum = item.Datum;
card.elevation = (int)item.ElevationFt;
card.description = item.Description;
card.dirLocation = item.DoLocation;
card.bbox = item.Bbox;
card.uniqID = item.UniqId;
values.Add(card);
}
CatalogResults response = new CatalogResults();
I just tried this:
var query2 = from item in context.Viewbostons
select new CatalogResults
{ item.Idx,
item.Product,
(long)item.SizeBytes,
item.Date.ToString(),
item.Type,
item.Classification,
item.Distributor,
item.EgplDate.ToString(),
(int)item.ClassificationInt,
item.Handling,
item.Creator,
item.Datum,
(int)item.ElevationFt,
item.Description,
item.DoLocation,
item.Bbox,
item.UniqId
};
But I get the following error:
Error 79 Cannot initialize type 'CatalogService.CatalogResults' with a
collection initializer because it does not implement
'System.Collections.IEnumerable' C:\Users\ysg4206\Documents\Visual
Studio
2010\Projects\CatalogService\CatalogService\CatalogService.svc.cs 91 25 CatalogService
I should tell you what the definition of the CatalogResults is that I want to return:
[DataContract]
public class CatalogResults
{
CatalogEntry[] _results;
[DataMember]
public CatalogEntry[] results
{
get { return _results; }
set { _results = value; }
}
}
My mind is dull today, apologies to all. You are being helpful. The end result is going to be serialized by WCF to a JSON structure, I need the array wrapped in a object with some information about size, etc.
Since .NET 3.0 you can use object initializer like shown below:
var catalogResults = new CatalogResults
{
results = context.Viewbostons
.Select(it => new CatalogEntry
{
idx = it.Idx,
product = it.Product,
...
})
.ToArray()
};
So if this is only one place where you are using CatalogEntry property setters - make all properties read-only so CatalogEntry will be immutable.
MSDN, Object initializer:
Object initializers let you assign values to any accessible fields or properties of an
object at creation time without having to explicitly invoke a constructor.
The trick here is to create a IQueryable, and then take the FirstOrDefault() value as your response (if you want a single response) or ToArray() (if you want an array). The error you are getting (Error 79 Cannot initialize type 'CatalogService.CatalogResults' with a collection initializer because it does not implement 'System.Collections.IEnumerable') is because you're trying to create an IEnumerable within the CatalogEntry object (by referencing the item variable).
var response = (from item in context.Viewbostons
select new CatalogEntry()
{
idx = item.Idx,
product = item.Product,
size = (long)item.SizeBytes,
...
}).ToArray();
You don't have to create anonymous types in a Linq select. You can specify your real type.
var query = context.Viewbostons.Select( it =>
new CatalogEntry
{
idx = it.idx,
... etc
});
This should work:
var query = from it in context.Viewbostons
select new CatalogEntry()
{
// ...
};