Comparing dataset with array of data asp net - c#

I have this page of operators that has two tables with data from database, one table with existing operators and one table with existing users. If a user is already an operator, I want that row to be hidden in the table. Here is my code:
C#:
var userDetailsList = new List<ContactPartial>();
TellusAPI.TellusUserDetails userDetails;
var operators = _administrationSystem.GetOperatorsInformation(userId); //DataSet
var getUser = webSearch.DoSearchForNameAndNumber(userId, txtSearchForOperator.Text, false, "", "", false, false, out userDetails); //Array
if (getUser == null)
{
userDetailsList.Add(new ContactPartial(userDetails));
rptAdd.DataSource = null;
}
else
{
var query = getUser.Where(y => y.ID == (long)operators.Tables[0].Rows[0]["ID"]).Select(x => x.ID);
var result = from o in operators.Tables[0].AsEnumerable()
where o.Field<int>("ID") == Convert.ToInt64(query)
select o;
rptAdd.DataSource = result;
}
Class:
/// <summary>
/// Class for converting.
/// </summary>
public class ContactPartial
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string CompanyName { get; set; }
public string Email { get; set; }
public ContactPartial(TellusUserDetails tud)
{
if (tud == null)
{
return;
}
Id = tud.UserID;
FirstName = tud.FirstName;
LastName = tud.LastName;
CompanyName = tud.Organisation.Description;
Email = tud.Email;
}
public ContactPartial(TellusSearchUserInfo tsi)
{
if (tsi == null)
{
return;
}
Id = tsi.ID;
FirstName = tsi.FirstName;
LastName = tsi.LastName;
CompanyName = tsi.CompanyName;
Email = tsi.Email;
}
}
So I now compare these two and filter the rows with ID, but it still reads all rows. Why?

Related

SearchString search operation with forename and surname

Hi I have one search box that takes searchstring and search it in the database. The input are like "utsav pal", "utsav p", "pal" for the account Name Utsav Pal. But I want to use only the initials like "uts pa". Can any one suggest me what can I do ?
Here is the logic I have used
if(!String.IsNullOrEmpty(vm.SearchString)){
accounts = accounts.where( a =>
((a.Forname != null) && (a.Forename +" "+ a.Surname).IndexOf(vm.SearchString, StringComparison.CurrentCultureIgnoreCase) >= 0 ) ||
((a.AccountCode != null) && a.AccountCode.IndexOf(vm.SearchString, StringComparison.CurrentCultureIgnoreCase) >= 0 )
You can use the logic below. I have created a Person model class and a repository class for the data based on the Person model.
The first code snippet is The Person Model and Repository Classes.
public class Person
{
public int Id { get; set; }
public string ForeName { get; set; }
public string SurName { get; set; }
public string Email { get; set; }
}
public static class Repository
{
public static List<Person> p { get; set; }
static Repository()
{
p = new List<Person>();
p.Add(new Person
{
Id = 1,
ForeName = "Nafi",
SurName = "Ahmadi",
Email = "Nafi#example.com"
});
p.Add(new Person
{
Id = 1,
ForeName = "Shafiq",
SurName = "Rahman",
Email = "Shafiq#example.com"
});
p.Add(new Person
{
Id = 1,
ForeName = "Wali",
SurName = "Mohammad",
Email = "Wali#example.com"
});
}
}
Here is the logic that uses initial letters to search in repository data.
static void Main(string[] args)
{
// Data set
List<Person> a = Repository.p;
// Search string
string SearchStringInput = "N A";
// Split search string
string[] SearchString = SearchStringInput.Split(" ");
if (!String.IsNullOrEmpty(SearchStringInput))
{
var p = a.Where(x =>
((x.ForeName != null)) && ((x.ForeName.ElementAt(0).ToString() == SearchString[0].ToString()) || x.ForeName.ToString() == SearchString[0].ToString()) && (x.SurName.ElementAt(0).ToString() == (SearchString[1].ToString())) || (x.SurName.ToString() == (SearchString[1].ToString())));
foreach (var item in p)
{
Console.WriteLine(item.Email);
}
}
Console.ReadKey();
}

Filter based on a string value in List<string> column in a table Entity Framework Core

I have a table with the following structure (code first approach using Entity Framework Core) in PostgreSQL
public class Product_Order
{
[Key]
public string product_number { get; set; }
public string customer_product_number { get; set; }
public List<string> product_statuses { get; set; }
public bool is_test { get; set; } = false;
public DateTime created_at { get; set; } = DateTime.UtcNow;
public DateTime updated_at { get; set; } = DateTime.UtcNow;
public string created_by { get; set; } = "system";
public string updated_by { get; set; } = "system";
}
Now, the product_statuses column usually contains of a list of statuses - ready, pickedup, scheduled, closed, cancelled.
I need to come up with a solution which returns me a list of product orders which DOES NOT CONTAIN orders which are closed or cancelled.
Here's the solution that I have at the moment which is not filtering as expected
_context.Product_Order.Where(t => t.is_test && !t.statuses.Contains("closed") && !t.statuses.Contains("cancelled")).ToList();
I think your code is ok for your data structure to find that information. I have created a dummy class and list to replicate your data and list. And I was able to find data by using you code. Sample Code given below what I have tested =>
void Test()
{
List<Product_Order> items = new List<Product_Order>();
var temp = new Product_Order() { product_number = "001", isTest = true };
temp.product_statuses = new List<string>();
temp.product_statuses.Add("good");
temp.product_statuses.Add("greate");
temp.product_statuses.Add("new");
items.Add(temp);
temp = new Product_Order() { product_number = "002", isTest = true };
temp.product_statuses = new List<string>();
temp.product_statuses.Add("good");
temp.product_statuses.Add("bad");
temp.product_statuses.Add("notnew");
items.Add(temp);
temp = new Product_Order() { product_number = "003", isTest = true };
temp.product_statuses = new List<string>();
temp.product_statuses.Add("n/a");
temp.product_statuses.Add("bad");
temp.product_statuses.Add("Closed");
items.Add(temp);
temp = new Product_Order() { product_number = "004", isTest = false };
temp.product_statuses = new List<string>();
temp.product_statuses.Add("n/a");
temp.product_statuses.Add("bad");
temp.product_statuses.Add("Cancelled");
items.Add(temp);
var finalOutput = items.Where(c => c.isTest == true && !c.product_statuses.Where(v => v.ToLower() == "closed").Any() && !c.product_statuses.Where(v => v.ToLower() == "cancelled").Any()).ToArray();
}
public class Product_Order
{
public string product_number { get; set; }
public bool isTest { get; set; }
public List<string> product_statuses { get; set; }
}
Finally , I think it is your data what not wright with you lambda expression. So, I modified for you a little bit.And that is
FINAL ANSWER:
var finalOutput = _context.Product_Order.Where(c => c.isTest == true && !c.product_statuses.Where(v => v.ToLower() == "closed").Any() && !c.product_statuses.Where(v => v.ToLower() == "cancelled").Any()).ToArray();
Please check my code and let me know.

Get selected column from iqueryable

How can I return fieldList from an IQueryable object?
// fieldList="Code,Name";
var result = from Activity in query
select new
{
Code = Activity.Code,
Name = Activity.Name,
StatusCode = Activity.ClaimStatus.Name
};
DTO
public class CustomDto
{
public string Code { get; set; }
public string Name { get; set; }
public string StatusCode { get; set; }
}
Convert To Dto
var result = items.AsQueryable().Select(x => new CustomDto()
{
Code = x.Code,
Name = x.Name,
StatusCode = x.ClaimStatus
}).ToList();

datarow filtering array with dataset c# asp net

I have this page of operators that has two tables with data from database, one table with existing operators and one table with existing users. If a user is already an operator, I want that row to be hidden in the table. Here is my code:
C#:
var userDetailsList = new List<ContactPartial>();
TellusAPI.TellusUserDetails userDetails;
var operators = _administrationSystem.GetOperatorsInformation(userId); //DataSet
var getUser = webSearch.DoSearchForNameAndNumber(userId, txtSearchForOperator.Text, false, "", "", false, false, out userDetails); //Array
if (getUser == null)
{
userDetailsList.Add(new ContactPartial(userDetails));
rptAdd.DataSource = null;
}
else
{
userDetailsList = getUser.Select(x => new ContactPartial(x)).ToList();
var dv = new DataView(operators.Tables[0]);
foreach (var Operator in userDetailsList)
{
if (!userDetailsList.Contains(Operator)) continue;
dv.RowFilter = "ID = " + Operator.Id;
}
rptAdd.DataSource = userDetailsList; //Repeater
}
Class:
/// <summary>
/// Class for converting.
/// </summary>
public class ContactPartial
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string CompanyName { get; set; }
public string Email { get; set; }
public ContactPartial(TellusUserDetails tud)
{
if (tud == null)
{
return;
}
Id = tud.UserID;
FirstName = tud.FirstName;
LastName = tud.LastName;
CompanyName = tud.Organisation.Description;
Email = tud.Email;
}
public ContactPartial(TellusSearchUserInfo tsi)
{
if (tsi == null)
{
return;
}
Id = tsi.ID;
FirstName = tsi.FirstName;
LastName = tsi.LastName;
CompanyName = tsi.CompanyName;
Email = tsi.Email;
}
}
So I now compare these two and filter the rows with ID, but it still reads all rows.
foreach (var Operator in userDetailsList) ******
{
if (!userDetailsList.Contains(Operator)) continue;
dv.RowFilter = "ID = " + Operator.Id;
}
I see some problem with naming convention .. You should corretly notify what is operator and what is user details object. Don't mix them up.
foreach (var user in userDetailsList)
{
// Since dv is the one which contain operator details
if (dv.Select("ID= '" + user.Id + "'").Count > 0)
{
dv.RowFilter = "ID = " + user.Id;
}
}

How can I create set contain id record in first set equal id record in second set?

In database I have two tables:
public partial class PersonOne
{
public int id { get; set; }
public string name { get; set; }
public string surname { get; set; }
}
public partial class PersonTwo
{
public int id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
}
I would like to fill my set:
public class PersonOnePersonTwo
{
public int PersonOneId { get; set; }
public int PersonTwoId { get; set; }
}
where PersonOne.name == PersonTwo.firstname && PersonOne.surname == PersonTwo.lastname but I have no idea how I can do that - because below code isn't efficient and is so slow:
List<PersonOne> personOneList = new List<PersonOne>();
List<PersonTwo> personTwoList = new List<PersonTwo>();
List<PersonOnePersonTwo> personOnePersonTwoList = new List<PersonOnePersonTwo>();
foreach (PersonOne personOne in personOneList)
{
foreach(PersonTwo personTwo in personTwoList.Where(x => x.firstname == personOne.name && x.lastname == personOne.surname).ToList())
{
personOnePersonTwoList.Add(new PersonOnePersonTwo
{
PersonOneId = personOne.id,
PersonTwoId = personTwo.id
});
}
};
Try this:
var result = personOneList.Join
(
personTwoList,
person1 => new { Key1 = person1.Name, Key2 = person1.Surname },
person2 => new { Key1 = person2.FirstName, Key2 = person2.LastName },
(person1, person2) => new PersonOnePersonTwo { PersonOneId = person1.Id, PersonTwoId = person2.Id }
).ToList();
I would go with:
var personOnePersonTwoList = new List<PersonOnePersonTwo>();
foreach (var personOne in personOneList)
{
personOnePersonTwoList = personTwoList.Where(x => x.firstname.Equals(personOne.name, StringComparison.OrdinalIgnoreCase) &&
x.lastname.Equals(personOne.surname, StringComparison.OrdinalIgnoreCase))
.Select(x => new PersonOnePersonTwo {PersonOneId = personOne.id, PersonTwoId = x.id}).ToList();
};
As a side note: it's more convinient to use Equals when comparing strings.

Categories

Resources