2015-11-04 35 views
0

我有一個userList需要從字符串代碼排除如下面C#LINQ用戶列表不是一個字符串

string[] userList={"gerald","admin","john"} ; 

List<EventItem> logall= new List<EventItem>(); 
List<EventItem> logfilteruser= new List<EventItem>(); 

logall= Records.AsQueryable().Select(e => new EventItem(e)).ToList(); 

logall將所有的記錄

如何排除userList?

以下是我的代碼排除userList。

if (users != "") { 
     foreach (var filter in userList) { 
      logfilteruser= logall.Where(x => !x.depict.Contains(filter)).ToList(); 
     } 

的描繪是在記錄一個字符串屬性,這有很多的記錄,以下是描繪字符串值

 depict= "username:\t\tgerald\r\n\t\t\t0x18ECC03\r\n\t"; 
    depict= "username:\t\tjason\r\n\t\t\t0x18ECC03\r\n\t"; 

之一,如果userList只有一個value(gerald),代碼works.When userList有多個它不工作。結果顯示所有記錄出來,這是不正確的。

我如何使用linq或其他方式修復代碼?感謝併爲我的英文破碎感到抱歉。

在這裏,我有我的參考EventItem和記錄

public class EventItem { 
    public int Id { get; set; } 
    public string LogName { get; set; } 
    public string depict{ get; set; } 

    public WinEventItem(EventRecord record) { 
     Id = record.Id; 
     LogName = record.LogName; 
     depict= record.FormatDescription(); 
    } 
} 

public List<EventRecord> Records { get; private set; } 
private Records = new List<EventRecord>(); 

回答

0

如果depict屬性是一個字符串,這將工作:

logfilteruser = logall.Where(x => !userList.Any(u => x.depict.Contains(u))).ToList(); 

從理論上講,這也應該執行比略好foreach (var filter in userList),因爲Any()只要在userlist中找到匹配項就會返回,它不需要每次枚舉整個集合(即使已經找到匹配項)。

編輯

這裏是表示本工作示例控制檯應用程序。

我創建了一個示例類,而不是從EventRecord繼承的完整類。它返回一個字符串FormatDescription,就像EventRecord所做的那樣。

class Program 
{ 

    static void Main(string[] args) 
    { 
     // Create some test data 
     List<TestEventRecord> Records = new List<TestEventRecord>(); 
     Records.Add(new TestEventRecord { Value = "username:\t\tgerald\r\n\t\t\t0x18ECC03\r\n\t" }); 
     Records.Add(new TestEventRecord { Value = "username:\t\tjason\r\n\t\t\t0x18ECC03\r\n\t" }); 

     string[] userList = { "gerald", "admin", "john" }; 

     List<EventItem> logall = new List<EventItem>(); 
     List<EventItem> logfilteruser = new List<EventItem>(); 

     // Contains all entries 
     logall = Records.AsQueryable().Select(e => new EventItem(e)).ToList(); 

     // Contains only 1 entry as the list is filtered 
     logfilteruser = logall.Where(x => !userList.Any(u => x.depict.Contains(u))).ToList(); 
    } 
} 

public class EventItem 
{ 
    public int Id { get; set; } 
    public string LogName { get; set; } 
    public string depict { get; set; } 

    public EventItem(TestEventRecord record) 
    { 
     Id = record.Id; 
     LogName = record.LogName; 
     depict = record.FormatDescription(); 
    } 
} 

// Example class for testing 
public class TestEventRecord 
{ 
    public int Id { get; set; } 
    public string LogName { get; set; } 
    public string Value { get; set; } 

    public string FormatDescription() 
    { 
     return Value; 
    } 
} 
+0

謝謝你讓我知道使用foreach的其他方式,但我試了幾分鐘。它看起來不起作用,它將最大記錄結果返回給我。我也嘗試使用包含第一個比使用expect或removeall – geraldlim

+0

你可以發佈你的'EventItem'模型嗎?你聲明'描述'是一個字符串,但可能有很多記錄。你是什​​麼意思? –

+0

對不起,我只是發佈我的EventItem模型 – geraldlim

0

您每次都在循環中重新過濾原始列表。請嘗試以下操作:

logfilteruser = logall; 

foreach (var filter in userList) 
{ 
    logfilteruser = logfilteruser.Where(x => !x.depict.Contains(filter)).ToList(); 
} 
+1

我會在循環之後加入'ToList'一次。在某些情況下(例如,當'logall'是'IQueryable'時),這將會更有效率。 – CompuChip

+0

我試過但也沒有工作,這段代碼總是把所有記錄都返回給我。不是每個記錄都有描述的問題,我的記錄有超過一百個? – geraldlim

相關問題