dynamically select fields in linq - linq

I have this here:
Dim query = FromTableRows.Select(Function(Row) Row.Item(_SqlSyntaxChecker.SelectedFields(0)))
Row is a normal DataRow, so I can get the field value of the rows like this: Row.Item(0), Row.Item(1), etc.
SelectedFields contains the field names of the FromTableRows.
Now, I would like to select all the fields in the SelectedFields list, while the number of the selected fields can vary.
Is this possible? How should I modify the code?
Thanks.

You can simply make use of the ItemArray property, if I understand your question correctly.
FromTableRows.Select(Function(Row) Row.ItemArray)
The ItemArray property is an object array that contains the elements found in the DataRow. You will, of course, lose any mapping via this method from columns to elements, but it sounds like that's what you want.

Related

Table Extraction in UIPath if table has images

I am trying to extract the Table which has the following format
When I want to extract i should have either put some character on place up icon or i dont want that either the case is fine..
But UIPath brings this way.. 78,59,237806 all as one text which is misleading.. How to resolve this issue..
Thanks
Take a look at the Find Children activity. You can specify an item via selector (the table) and use Find Children to return a collection of type UiElement.
So set the filter to extract the "<webctrl tag='tr' />" which will effectively give you a collection of the rows.
Use a For Each to iterate through each UiElement you got from the first Find Children activity, and use that element to run another Find Children. In this case, set the filter to extract the elements with a class of "mid-wrap". This gives you a collection of the elements in the row which match that requirement, and this will exclude the data-up value, since that's a different class.
You can then loop over this collection to get the innertext attribute, which will give you the actual values you're looking for each cell in the row. Use something like Add Data Row to add the values to a datatable, and let the For Each run over the next row in the collection.

How to allow user to add information for every fields in forms

i have a strange user request: they want to be able to say for every fields in every forms from where they get information used to compile it.
value must be selected from a list
essentially iam looking a way to avoid to double the fields.
one way can be add a M:M table where link field-name and information-source
but using this for retrive from real data table only fields derived from an info-source can be really bad
EDIT
to be more clear the solution i want to avoid is to have a data table like this:
fieldname1
fieldname1Origin
fieldname2
fieldname2Origin
fieldname3
fieldname3Origin
...
but i do the link table in this way:
FieldName
OriginId
i have to map oll field names and to search in it with string name when i need to query data table i have to do something like this
select
case when (exists select 1 from LinkTable where FieldName='fieldname1' AND
OriginId=SelectedID) then fieldname1
else NULL
end
for every fields
What about mixing several controls, such as a combobox with all lines, that would trigger the display of all columns of that line.
Something like that (I am currenlty working on that project):

Concatenating a LINQ query and LINQ sort statement

I'm having a problem joining two LINQ queries.
Currently, my (original) code looks like this
s.AnimalTypes.Sort((x, y) => string.Compare(x.Type, y.Type));
What I'm needing to do is change this based on a date, then select all data past that date, so I have
s.AnimalTypes.Select(t=>t.DateChanged > dateIn).ToList()
s.AnimalTypes.Sort((…
This doesn't look right as it's not sorting the data selected, rather sorting everything in s.AnimalTypes.
Is there a way to concatenate the two LINQ lines? I've tried
s.AnimalTypes.Select(t=>t.DateChanged > dateIn).ToList().Sort((…
but this gives me an error on the Sort section.
Is there a simple way to do this? I've looked around and Grouo and OrderBy keep cropping up, but I'm not sure these are what I need here.
Thanks
From your description, I believe you want something like:
var results = s.AnimalTypes.Where(t => t.DateChanged > dateIn).OrderBy(t => t.Type);
You can call ToList() to convert to a List<T> at the end if required.
There are a couple of fundamental concepts I believe you are missing here -
First, unlike List<T>.Sort, the LINQ extension methods don't change the original collections, but rather return a new IEnumerable<T> with the filtered or sorted results. This means you always need to assign something to the return value (hence my var results = above).
Second, Select performs a mapping operation - transforming the data from one form to another. For example, you could use it to extract out the DateChanged (Select(t => t.DateChanged)), but this would give you an enumeration of dates, not the original animal types. In order to filter or restrict the list with a predicate (criteria), you'd use Where instead.
Finally, you can use OrderBy to reorder the resulting enumerable.
You are using Select when you actually want to use Where.
Select is a projection from one a collection of one type into another type - you won't increase or reduce the number of items in a collection using Select, but you can instead select each object's name or some other property.
Where is what you would use to filter a collection based on a boolean predicate.

How to filter custom columns from SharePoint list using LINQ?

My requirement is to get items, only from custom-columns, in a SharePoint list using LINQ.
Since my custom columns are created dynamically based on some calculations done on another SPList, it keeps increasing and decreasing in the count frequently, therefore I cannot use SPMetal.
I need to include a condition (!(SPBuiltInFieldId.Contains(field.Id))) to check if the items are taken only from custom fields in the following query.
List<SPListItem> AllResponses = (from SPListItem Response in oList.Items
select Response).ToList();
Please advice. Thanks!
I'm not sure it's possible to have a list item that contains only custom columns. Even if Title is not there, you'll have ID, Modified, Created, etc. Plus, there will be a number of hidden built-in columns.
If you want a list of items that contain items with custom fields, you can try something like the following, which utilizes Where and Any methods:
List<SPListItem> AllResponses =
(from SPListItem Response in oList.Items select Response)
.Where(item => item.Fields
.Cast<SPField>()
.Any(field => !SPBuiltInFieldId.Contains(field.Id)))
.ToList();
This will return all items that have at least one custom field.

Set column type dynamically in LINQ

I want to set the column names of an output from LINQ dynamically. Like so:
summary Rows.Field<Type>("Name")
I need to do this because I have to do the order by on three columns based on the a condition and each column have 2 different types like float, int and double
Does anyone have any suggestions on how this can be done?
LINQ to SQL deals with static types, though there are some tricks you can do. You can't change the type of the field on the fly, other than to use Convert.ToInt32, etc. to change the appropriate type to the value you need at the time you want to cast it.
Alternatively, you could try casting the value to each of the three types from the query:
from c in ctx
select new
{
IntVal = c.Value,
DoubleVal = Convert.ToDouble(c.Value),
.
.
}
This way, you'd have all three cast appropriately and can suck in the correct field. When working with the record, you can even use a wrapper around reflection to get something similarly to what you want. I don't know of Convert is LINQ supported, but you should be able to do something similarly.
HTH.

Resources