Inject property into existing List with LINQ [closed] - linq

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 12 years ago.
I have a Generic List of objects. Those objects have 4 properties. 3 are set by LINQ earlier in the app. Is it possible to insert the 4th property into the existing List using LINQ to SQL without looping through each object in the List?
EDIT
For reference sake, one of the first properties is an ID on the record, so I will know with each object in the List what the 4th property should be in the database, but I was hoping to do it without a For Loop as the List might be rather huge.

I'm not sure I 100% understand, but in your LINQ query you could do something like this:
var result = from r in Repository
select new MyType()
{
Value1 = r.Value1,
Value2 = r.Value2,
Value3 = r.Value3,
Value4 = "MyValue",
}
Untested, but the general idea should work.

Related

Comparison of LINQ to SPARQL? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm not a MS.NET person but got curious about LINQ, and this article http://www.linqpad.net/WhyLINQBeatsSQL.aspx explains very well why it's better than SQL.
I work a lot with SPARQL and in many respects it's worse than SQL (even 1.1 is a bit immature IMHO). Is there a comparison of LINQ to SPARQL in the style of the above article?
I think the LINQ aspect that's most interesting for RDF data is that LINQ can return hierarchical structures (in RDBMS speak that's table-valued variables; or think XML structures). SPARQL cannot do that:
you can't make CONSTRUCT subqueries, see GroupGraphPattern
and SubSelect in the grammar
if you try to return an array of complexly CONSTRUCTed objects, I bet you'll get a mess. Nor I believe you can mix arrays & hierarchical structures several levels deep. If you disagree, see this http://vocab.getty.edu/doc/queries/#All_Data_For_Subject and try to write a query to return it for all ?s gvp:broaderExtended aat:300264089 (disclaimers: I built that endpoint)
So with SPARQL we either return tabular data, or a single graph object but can't mix them freely. Which is ironic, because RDF is a graph data model.
There are several LINQ to SPARQL bindings:
https://github.com/Efimster/LINQtoSPARQL
http://rdfsharp.codeplex.com/
http://code.google.com/p/linqtordf/
http://www.dotnetrdf.org/
http://brightstardb.com
https://github.com/semiodesk/trinity-rdf
But does any of them handle this "hierarchical structures" aspect?
(I prefer this to be a comment but it is too too long)
I am not an expert, neither in LINQ nor SPARQL, and I didn't check the links you've provided, but recently I've listened to a Podcast with Anders Heilsberg, Microsoft Technical Fellow, and Lead Designer of C# and LINQ.
http://www.se-radio.net/2008/05/episode-97-interview-anders-hejlsberg/
He said that LINQ syntax follows a FROM ... WHERE ... SELECT structure to allow for better type inference.
When it knows the table name (FROM ... clause) the type inference engine in LINQ can start working at compile time, to fetch the column names for the autocompleter, and e.g interact with the syntax checker in Visual Studio.
I don't think this is easy/possible for a RDF triple where the property/predicate (SELECT /WHERE) can even be empty, but not FROM, (Blank Nodes).
These Blank Nodes denote existence of an individual with specific attributes, but without providing an identification or reference).

Build LINQ Select conditionally similar of using PredicateBuilder for Where clause [duplicate]

This question already has answers here:
LINQ : Dynamic select
(12 answers)
Closed 8 years ago.
I'm working with dynamic queries using LINQ on Entity Framework.
To query some tables by user input filters, we are using PredicateBuilder to create conditional WHERE sections. That works really great, but the number of columns returned are fixed.
Now, if we need the user to select which columns he needs in their report, besides their filters, we are in trouble, as we don't know how to do dynamic myQuery.Select( x => new { ... }) as we do for Where clause.
How can we achieve something like this?
A bit of searching reveals that this is tricky. Anonymous types are created at compile time, so it is not easy to create one dynamically. This answer contains a solution using Reflection.emit.
If possible, I would recommend just returning something like a IDictionary<,> instead.

Does OO fit this? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I have this process where in one table we have a series of item movements that are to be
applied to the other table with items & stocks. Basically, the item movements table is mapped by the following Entity:
public class ItemMovement {
enum ItemMovementStatus { NEW, APPLIED }
private Long movementId;
private String itemName;
private String itemCategory;
private String arbitraryQualifier;
private Date movementDate;
private ItemMovementStatus status;
// getters & setters
}
The inventory item entity is like this:
public class InventoryItem {
private String itemName;
private Double itemStock;
private String arbitraryQualifier;
// getters & setters
}
Movements are generated in the course of a month, at the end of which all the movements must be "applied" to the inventory table. "Applying" a movement means essentially substracting the ITEM_MOVEMENT_QTY for every given movement from the STOCK value of the inventory table where an exact match exists. If an exact match exists with at least the required movement quantity, the job is done. If not, we just take what we can and continue taking now from another item which falls into the same ITEM_CATEGORY. If this last inventory item did not have enough to complete the requested movement quantity we must take from inventory items that share the same ARBITRARY_QUALIFIER that the movement item has.
The problem with this last is that, matches for this ARBITRARY_QUALIFIER can go from hundreds or even thousands of "matches" per movement item, because, as it names implies, this qualifier can relate two "totally" unrelated items. In the worst case (very worst), although very remotely, it should be possible that ALL inventory items are a match for a given item movement.
Initially I wanted to retrieve all matches (in chunks, i.e. the query is "paginated") like this:
select m,i from ItemMovement m, InventoryItem i where
m.itemname=i.itemname
or m.itemCategory=i.itemCategory
or m.arbitraryQualifier=i.arbitraryQualifier
then process each movement with ALL of its matches in a very OO oriented approach, but this is taking too much time when having more than 20K of movements and 20K inventory items. I can actually SEE that the query that retrieves the data (even when paginated) takes too much time (more than a minute per page). Taking too much time is NOT the problem per se, but a constraint that forbids me to hold the transaction for more than 10 min. I know I can increase this value, but I would like to know if the approach I am taking is the right one.
I wish of course to keep a very OO approach in order to fully control the transition of the item movement and keep the arithmetic very clear. I believe the "real" question here is:
¿Is OO the right "way" to do this "kind" of process? If so, is there a design pattern which better addresses my problem in terms of performance, mantainability of the code?
¿Is this a process where I should look, for example, for a PL/SQL stored procedure instead?
I am using EJB, JPA(Hibernate). The database is Oracle.
Thanks,
hibernate was not designed for batch operations.
You will be able to get better performance using PL/SQL procedures than you can get with hibernate.
You must consider the entire system architecture when deciding whether to do it with java or with PL/SQL. In most cases it is possible to get sufficient performance for batch jobs with hibernate.
Regardless of what you choose, there are often ways to improve performence within the given architecture.
One very impotant thing to consider with hibernate is how you access your objects. You want to load the objects from the database using a small number of SELECT operations. Your job will most likely go quicker if you load all objects using 2 SELECT (one for each object type) than if you use 20000 (one for each movement).

Linq query giving Invalid column name "xyz" error [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am using this LINQ query
var db = new XYZ();
var product = (from cp in db.CatalogProducts
where cp.ProductID == productId
select cp).FirstOrDefault();
Running this query gives me an error
Invalid column name 'Cracker Cruncher'.
Invalid column name 'crushing torque'.
Invalid column name 'Slicing velocity'.
Can any one help me in this matter?
Gautam
Issue Resolved.. Just now I checked that Some1 has made drastic changes to DB without informing me.. Thanks for keeping up with me

Rails ActiveRecord Wildcard + Interpolation Issue [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I have been tried the following query and it succeeds (only returns the one record with exact match):
def self.search(query)
where("name like ?", query)
end
SQL Executed:
SELECT `products`.* FROM `products` WHERE (name LIKE 'Game')
This query also succeeds (It returns multiple records and there are multiple records with the words 'Game' in their name):
def self.search(query)
where("name like ?", "%Game%")
end
SQL Executed:
SELECT `products`.* FROM `products` WHERE (name like '%Game%')
However when I attempt place a wildcard character with interpolation:
def self.search(query)
where("name like ?", "%#{query}%")
end
SQL Executed:
SELECT `products`.* FROM `products` WHERE (name like '%[\"Game\"]%')
It doesn't return anything. Knowing me...probably missing a comma or something. Thanks in advance...
You are passing an Array instead of a String.
Try:
def self.search(query)
where("name like ?", "%#{query[0]}%") unless query.empty?
end
If it succeeds then fix the query input to be sent as string and not an Array.

Resources