开发者

find property value from List<Postavke>() with linq

I have class "Postavke" and then i store it into

List<Postavke> postavke = new List<Postavke>(); 

Now i want to find some elemnt (property) from this List. I know "Name", "Surname" and i want to get "Address".

How to get "Adress" if i know "Name" and "Surname". All this are properties in "Postavke" class

whole class

public class Postavke
    {
        #region Properties

        public string Name { get; set; }
        public string Surname { get; set; }
        public string Address { get; set; }

    开发者_运维问答    #endregion

        #region Methods

        public Postavke(string name, string surname, string address, string oznakaLokacije, string oznakaZapore)
        {
            Name = ean;
            Surname = surname;
            Address = address;
        }

        #endregion
    }


You can query postavke for all results that contain the name and surname and put the results into a list. Putting the results into a list I find makes things easier to validate and handle unforseen items as from the looks of it it is possible that duplicate items could appear.

if the results must have all data within the list item then:

List<Postavke> results = new List<Postavke>();

var query1 = from a in postavke
             where a.Name == searchName
             && a.Surname == searchSurname
             select a;

results.AddRange(query1);

this list will have all the results that contain the exact name and surname. If you just want the address then you can use:

List<string> results = new List<string>();

var query1 = from a in postavke
             where a.Name == searchName
             && a.Surname == searchSurname
             select a.Address;

results.AddRange(query1);

this will produce a list of addresses. From here you can then validate the list if you want by doing such things as checking to see how many items in the list there are so you know how you want to handle it etc.

If you want to use just either the name or the surname then you can remove the line that asks for one or the other.

If you end up with duplicates but the results are the same then you can change the line

results.AddRange(query1);

to

results.AddRange(query1.Distinct());

by using the Distinct() method it will sort the query and remove duplicates so the list will not be in the same order as it would if you didn't use it.

If you only wanted one result then it's worth validating it by checking how many items are in the list by using

results.Count

you could if it equals 1 carry on, otherwise throw up a message or some other way you may want to handle it. Other pieces of validation that are good is to set values ToLower() and Trim() during the search as to avoid actually changing the original text.

So taking the last query as an example:

List<string> results = new List<string>();

var query1 = from a in postavke
             where a.Name.ToLower().Trim() == searchName.ToLower().Trim() 
             && a.Surname.ToLower().Trim()  == searchSurname.ToLower().Trim() 
             select a.Address;

results.AddRange(query1);

you can also filter the query with the where clause after you make the query by doing:

List<string> results = new List<string>();

var query1 = from a in postavke
             select a.Address;

query1 = query1.Where(h => h.Name == searchName);
query1 = query1.Where(h => h.Surname == searchSurname);

results.AddRange(query1);

The 2 where clauses were split to show you can perform where clause at different points so you could have where clauses within if statements. you can combine the 2 into:

query1 = query1.Where(h => h.Name == searchName && h.Surname == searchSurname);

Hopefully this helps


This will work if you can be sure there's exactly one match

var address = poatavke.Where(p=>p.Name == name && p.Surname == surname).Single().Address;

If you don't know if there's no matches or exactly one you can do:

var posta = poatavke.Where(p=>p.Name == name && p.Surname == surname).SingleOrDefault()
var address = posta == null ? string.Empty : posta.Address;

if you don't know how many matches there's going to be but always want the first (or are using ET which doesn't understand Single())

var posta = poatavke.Where(p=>p.Name == name && p.Surname == surname).FirstOrDefault()
var address = posta == null ? string.Empty : posta.Address;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜