Updating fields in one list from another list [closed] - c#

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 5 years ago.
Improve this question
Is there a better or more efficient way to do this?
Both of these objects have the same Id and some similar fields that I want to update.
foreach (var i in oldItems)
{
foreach (var j in newItemValues)
{
if(i.id == j.Id)
{
j.field1 = (decimal)i.field1;
j.field2 = (decimal)i.field2;
}
}
}

try this maybe you are like this;
foreach (var n in newItemValues)
{
Item item = oldItems.FirstOrDefault(x => x.Id == n.Id);
if (item != null)
{
n.field1 = item.field1;
n.field2 = item.field2;
}
}
Edit: If you keep oldItems in a HashSet or HashTable by keeping key as Id the time taken will slow to O(N).

Convert oldItems or newItemValues into a Map before looping.
That will reduce computational complexity from O(n^2) to O(n)

You can do this by Linq Query
newItemsValues =
(from t1 in oldItems
join t2 in newItemsValues on t1.Id equals t2.Id
select new YourModel { field1 = t1.field1 ,field2 = t1.field2 }
).ToList();
result will a list of all matching items where oldItems Id will be equals to newItemsValues Id

Related

ASP.NET using Entity Framework display data to drop down list with distinct [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Please I need to implement distinct in a.CompanyName but it doesn't work. What do I need to do ?
try
{
var listCompany = company.CompanyList();
companyList1.DataSource = listCompany.Select(a => new { a.CompanyName, a.CompanyId }).Distinct();
companyList1.DataTextField = "CompanyName";
companyList1.DataValueField = "CompanyId";
companyList1.DataBind();
}
catch (Exception)
{
throw;
}
You can use with .GroupBy()
Groups the elements of a sequence.
companyList1.DataSource = listCompany
.GroupBy(x => x.CompanyName)
.Select(a => new
{ CompanyName = a.First().CompanyName, CompanyId = a.First().CompanyId });
Above query will group your result by company name and then will select CompanyName and CompanyId of first record
Try out online

Get unique items from multiple lists using LINQ- C# [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 7 years ago.
Improve this question
I have 3 lists.
ListA<items>
ListB<items>
ListC<items>
I have to select one item from each list but the item.itemID should be unique for each of those items. How can i achieve this? Thanks in advance.
List<items> concat = new List<items>();
concat.AddRange(ListA);
concat.AddRange(ListB);
concat.AddRange(ListC);
List<items> result = new List<items>();
foreach (var item in concat)
{
if (result.Where(x => x.itemId == item.itemId).Count() == 0)
{
result.add(item);
}
}
//result should now contain what you are looking for
So you want exactly one item from each list, where that item hasn't been selected already from a previous list?
Then select from the lists where the ID isn't in the previous selection. Maybe something like this:
var itemA = listA.First(); // any item is unique, since this is our first one
var itemB = listB.First(b => b.ID != itemA.ID);
var itemC = listC.First(c => c.ID != itemA.ID && c.ID != itemB.ID);
If the count of lists isn't known then we'd need to make this a little more dynamic. Maybe something like this:
var selectedItems = new List<Item>();
foreach (var list in listOfLists)
selectedItems.Add(list.First(x => selectedItems.Count(y => y.ID == x.ID) == 0));
What this does is loop through the "list of lists" (since the number of lists isn't known, it must be in a collection data structure) and get the first item from each one where the currently known selected items do not have a matching ID. This should result in one selected item from each list.
(This all assumes that the lists contain a valid item that you're looking for. If that's not the case, you might use FirstOrDefault() instead and check for nulls.)
public class items
{
public int id {get;set;}
}
void Main()
{
var l1=new List<items>{new items {id=1}, new items {id=2}, new items {id=3}};
var l2=new List<items>{new items {id=2}, new items {id=3}, new items {id=4}};
var l3=new List<items>{new items {id=1}, new items {id=2}};
var result=l1
.Join(l2,(k1)=>true,(k2)=>true,(a1,a2)=>new {a1,a2})
.Join(l3,(k1)=>true,(k2)=>true,(b1,b2)=>new {b1.a1,b1.a2,a3=b2})
.Where(rec=>rec.a1.id!=rec.a2.id && rec.a2.id!=rec.a3.id && rec.a1.id!=rec.a3.id)
.First();
Console.WriteLine("{0},{1},{2}",result.a1.id,result.a2.id,result.a3.id);
}
Of the possible answers (132,142,231,241,321,341,342) it will pick and return the first one (132).
Example code: http://csharppad.com/gist/98a076bdd4e01dbd82be

Hash Join Algorithm [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 8 years ago.
Improve this question
I have two tables, each one stored in a List.
The table has two columns (thus arrays in lists are of size two)
List<int[]> tablea;
List<int[]> tableb;
Content of tablea
a b
{1,2}
{3,4}
{5,6}
content of tableb
b c
{2,3}
{2,4}
{6,7}
Now I wish to join two tables by field b, hence the result is
a b c
{1,2,3}
{1,2,4}
{5,6,7}
I know I can do this is sort merge join algorithms, and the algorithms is quite intuitive.
But I heard hybrid hashjoin(mentioned here http://en.wikipedia.org/wiki/Hash_join#_note-1) is faster.
May I know how to use hybrid hashjoin algorithms to join tablea and tableb in C#(Preferably) or java?
(What I concern here is speed rather than space)
The ode that can handle non-unique key values, would be like this:
Dictionary<int, List<int[]>> hashA = new Dictionary<int, List<int[]>>();
foreach (int[] a in tablea) {
List<int> list;
if (!hashA.TryGetValue(a[1], out list) {
list = new List<int>();
hashA.Add(a[1], list);
}
list.Add(a);
}
List<int[]> result = new List<int[]>();
foreach (int[] b in tableb) {
List<int[]> a;
if (hashA.TryGetValue(b[0], out a) {
foreach (int[] line in a) {
result.Add(new int[] { line[0], line[1], b[1] });
}
}
}

List<object> compare [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 9 years ago.
Improve this question
I have searched for a few examples and not found one that is similar to what I have and what I want to achieve.
I have 2 lists
class object1
{
string obj1_name;
int obj1_qty;
}
List<object1> listA
class object2
{
string obj2_name;
int obj2_qty;
}
List<object2> listB;
Now, using ListA as the primary list I want to see if ListB contains an object with the same name and if so, what is the quantity and hence, does the obj1_qty = obj2_qty each other? if not, there is a difference and I need to show it, most likely in a 3rd list which would be the difference, of the qty's if they exist. Note, ListA can be bigger or smaller than ListB
Show All List A (master list contains all objects)
Show the difference between those names/qty's that exist in both lists.
Gracias
Souncs like a join would work for you:
var query = from a in listA
join b in listB on a.obj1_name equals b.obj2_name
where a.obj1_qty != b.obj2_qty
select new {
Name = a.obj1_name,
QtyA = a.obj1_qty,
QtyB = b.obj2_qty,
Diff = a.obj1_qty - b.obj2_qty
};
The simpelest way to do this is just use a join from Linq
var items = from l1 in listA
join l2tmp1 in listB on l1.obj1_name equals l2.obj2_name into l2tmp2
from l2 in l2tmp2.DefaultIfEmpty();
select new {
ItemA = l1,
ItemB = l2,
Name = l1.obj1_name,
Difference = (l2 == null) ? 0 : l1.ob1_qty - l2.ob2_qty
};
items will now hold a IEnumerable of a anonymous class that holds the a reference to the item in ListA, a reference to the item in listB, the matching name, and the difference.

How return Tuple<T1,T2> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I create method where get anonymous types. I want return Tuple. How implement this?
public IEnumerable<Tuple<T1, T2, T3>> GetFiles()
{
using (TestEntities context = new TestEntities())
{
var query = from pf in context.T1
join pfExt in context.T2 on pf.Id equals pfExt.ProcessedFilesID
join st in context.T3 on pfExt.WFStatusID equals st.WFStatusID
select new
{
pf.Id,
pf.RecordCount,
pf.Name,
pfExt.PackageID,
StatusName = st.Name,
pfExt.ProtocolStatus
};
}
}
Something like the following should work:
public IEnumerable<Tuple<T1, T2, T3>> GetFiles()
{
using (TestEntities context = new TestEntities())
{
var query = from pf in context.T1
join pfExt in context.T2 on pf.Id equals pfExt.ProcessedFilesID
join st in context.T3 on pfExt.WFStatusID equals st.WFStatusID
select new { pf, pfExt, st };
return query.AsEnumerable()
.Select(x => Tuple.Create(x.pf, x.pfExt, x.st));
}
}

Categories

Resources