Add new record to linq query result - c#

I have a query as follows:
var paymentInfo =
from i in dbconnect.tblPayments
where i.tenderId == _tenderId
select i;
This query has some results, but I need to add an additional result that I already have, from the variable PaymentInfo.
For example suppose that my query has 2 results i need to add another result to "PaymentInfo" using linq.
I thought that the result is a kind of list, and that I could call .Add(PaymentInfo), but this doesn't work
How can I do this?

You can use Concat to concat another sequence to the end of this one.
var paymentInfo = paymentInfo.Concat(someOtherPayments);

I thought that the result is a kind of list
No, the result is an IEnumerable<T> which is read-only. You can create a list by calling .ToList() and then add an item to it.
var paymentInfo = (from i in dbconnect.tblPayments
where i.tenderId == _tenderId
select i).ToList();
paymentInfo.Add(existingPayment);

Related

Get ICollection out from IQueryable<ICollection> LINQ Query

I'm trying to write a query that grabs a list of countries out from my joined data.
Places is List<Country>.
var zonedCountries = (from dz in db.DeliveryZones.Include(d => d.Places)
where model.DeliveryZones.Contains(dz.ID)
select dz.Places);
I would expect zonedCountries to be a List but instead it is a IQueryable<ICollection<Country>>.
How do I extract the list from this?
If you want to get flattened list of countries:
var zonedCountries = (from dz in db.DeliveryZones.Include(d => d.Places)
where model.DeliveryZones.Contains(dz.ID)
from p in dz.Places
select p);
Or use SelectMany:
var zonedCountries = db.DeliveryZones.Include(d => d.Places)
.Where(dz => model.DeliveryZones.Contains(dz.ID))
.SelectMany(dz => dz.Places);
BTW I'm not sure if you need to include places manually in this case (thus you are selecting places instead of delivery zones). And you will probably want to select distinct countries only - Distinct() will help you here. Also if you want to store results in list, then simple ToList() call will do the job.

Checking the contents of a LINQ query that is being added to

I'm creating a LINQ query that needs to check on the contents before adding something from the list, so what I have is this
var foo = (from f in list1
from p in list1.list2
from m in p.Bar
let t = m.Type
let c = someMethod(t)
where c.Type == type && !foo.Contains(p)
select p).ToList();
the !foo.Contains(p) is not allowed, so is there a way of checking the query as it goes along or before the ToList() should I just add Distinct() to do the same as the condition?
There is no way to access the query as it is being built in the manner you are doing. If you want to ensure that a particular value only appears once in the output then Distinct is the best approach

Add single item to linq to sql query

I would like to add a single item to the results of a linq query. I know it's not possible to join a local source and a SQL source. So, is it possible to construct a query to do the same as this?
SELECT ID FROM Types
UNION
SELECT 1
The best I've come up with is this:
List<int> OrgList = DBContext.Types.Select(b => b.ID).ToList();
OrgList.Add(1);
but I'd rather add the item beforehand and still have an IQueryable. Or is there a good reason to not do it this way?
You must get the data from the db and then add the new item like you have done in your code.
The only way to have an IQueryable would be to deffer the adding of the new item to the point were the query is resolved.
You can use Union:
var query = DBContext.Types.Select(b => b.ID).ToList().Union(new[]{1});
Not tested but it should work
Try Concat
var result = DBContext.Types.Select(p => p.ID)
.Concat(new List<int>() { 1 }).ToList();

Prevent new values from LINQ query

I have some data in a List I want to make a query against. In the meantime however, other users can add to this List and I get wrong items in return:
var query = from s in selected
where s.contains("www")
select s);
Then a user Can add item to selected list before The query is run, and I Will get this also. Can I prevent this behaviour?:
selected.add("123www")
foreach (var s in query)
/// gives me 123www
The var "query" just has the query assigned to it, but the query itself is first performed when the query is accessed in for example a foreach loop - hence you get the newly added data.
If you don't want this, you can use an extension method like "ToList()", where the collection stays the same:
var queryResultList = (from s in selected
where n.contains("www")
select s).ToList();
Here the ToList() iterates the collection immediately, and you can now iterate the queryResultList and get the right results and even though new elements arrive the output stays the same.
The query represents an action, which can be triggered any time you want
and it will yield different results if the source of data changed.
Just make a "snapshot" of the results at the time you desire to do so with a .ToArray():
var query = from s in selected
Where s.contains("www")
Select s)
string[] snapshot = query.ToArray();
When you call .ToList() or .ToArray() the query is executed. Therefore to avoid your problem you can write this code:
var query = from s in selected
where s.Contains("www")
select s);
var result = query.ToList();
selected.Add("123www");
foreach(var item in result)
{
/* You won't see "123www" */
}

Return Multiple Columns in Linq to Sql?

How do I return multiple columns with linq to sql in C#?
I tried to end my query with
select new { A.Product, A.Qty };
but this returns some anonymous type and I am not sure what the heck what to do with this, How to return it and how to extract information out of it. I want to put it in some sort of array.
thanks
Are you trying to return the data from a method?
If so, you should just end the query with select A, which will produce the same type that A is.
If not, you can use the anonymous type the same way you use a regular type.
For example:
var results = from ... select new { A.Product, A.Qty };
foreach(var thing in results) {
Console.WriteLine("{0}: {1}", thing.Product, Thing.Qty);
}
EDIT: To make it into a list, call ToList, like this:
var resultsList = results.ToList();
Call tolist() on the query

Categories